servlet+jsp+java實現Web應用

來源:互聯網
上載者:User

標籤:width   test   span   mic   lips   static   pos   rate   ram   

servlet+jsp+java實現Web應用

環境:

1,eclipse
2,tomcat
3,eclipse tomcat 外掛程式

開發過程:

1,建立一個Dynamic Web Project

2,建立一個歡迎頁面

頁面可以是jsp/html,我們選擇一個jsp頁面(放在WebContent內)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body><h1>Do you come in?</h1><form method="post" action="hello.do">    Select:<br>    <select>        <option>yes        <option>no    </select>    <center>        <input type="submit">    </center></form></body></html>

3,向工程添加一個servlet檔案

package com.example;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class Welcome */@WebServlet("/Welcome")public class Welcome extends HttpServlet {    private static final long serialVersionUID = 1L;    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        response.setContentType("text/html");        PrintWriter out = response.getWriter();        String c = request.getParameter("select");        if(c.equals("yes"))            out.print("Welcome!");        else            out.print("I don‘t like you!");         //帶參跳到另一個頁面         request.setAttribute("token", token);         request.getRequestDispatcher("welcome.jsp").forward(request,response);    }}

4,建立welcome.jsp頁面,並接受參數值

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title><%String token = request.getParameter("token");//用request得到 %><style>          #ifr_fr{              width: 84%;              height: 700px;          }      </style>  </head><body >welcome to rzzx1.<iframe id="ifr_fr" name="ifr_fr" frameborder="0"  src=""></iframe></body></html>

5,建立一個web.xml

      web.xml用來建立servlet與jsp的關係(需要放在WEB-INF內)。

   根據不同的url來調用不同的servlet來進行處理。

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">  <display-name></display-name>  <servlet>    <servlet-name>Welcome</servlet-name>//要與下面的名稱相同    <servlet-class>com.example.Welcome</servlet-class>//調用的類的位置   </servlet>   <servlet-mapping>    <servlet-name>Welcome</servlet-name>    <url-pattern>/hello.do</url-pattern>//url標識  </servlet-mapping></web-app>

6,測試地址:http://localhost:8080/webtest/hello.do

7,可能會提示:HTTP method GET is not supported by this URL

解決方案:

public void doPost(HttpServletR……改成:public void service(HttpServletR……

 

 

 

servlet+jsp+java實現Web應用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.