servlet+jsp+java实现Web应用
环境:
1,eclipse
2,tomcat3,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"%>Insert title here Do you come in?
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"%>Insert title here <%String token = request.getParameter("token");//用request得到 %> welcome to rzzx1.
5,创建一个web.xml
web.xml用来建立servlet与jsp的关系(需要放在WEB-INF内)。
根据不同的url来调用不同的servlet来进行处理。
Welcome //要与下面的名称相同com.example.Welcome //调用的类的位置Welcome /hello.do //url标识
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……