servlet 中 service ,doGet , doPost 关系

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>servlet-test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
      <servlet-name>MySer</servlet-name>
      <servlet-class>test1.MySer</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>MySer</servlet-name>
      <url-pattern>/servlet/MySer</url-pattern>
  </servlet-mapping>
  
</web-app>

mySer


package test1;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MySer extends HttpServlet{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
        
        System.out.println("fa");
        if(request.getMethod().equals("POST")){
            doPost(request, response);
        }
        //一个servlet默认会执行service方法,service方法中会转向doget或者dopost(doget,dopost如果被子类覆盖,
        //那个就执行子类的。如果你在子类中覆盖了service 方法,那么是否执行doget或者dopost ,就成为service方法
        //的代码问题了)
    }
    
    
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
        System.out.println("get");
    }
    
    
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        System.out.print("post");
        request.setAttribute("msg", "登陆成功");
        request.getRequestDispatcher("/success.jsp").forward(request, response); 
    //response.sendRedirect("/success.jsp");
    //重定向是客户端发出的请求,所以是response请求,url地址栏改变
    //请求转发是服务端发出的,所以是request请求,url地址栏不变
} }

 

 

index.jsp

<?xml version="1.0" encoding="utf-8" ?>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insert title here</title>
</head>
<body>
    <form action="/servlet/MySer" name="f" method="post">
    <input type="text" value="" name="usename"/>
    <input type="password" value="" name="password"/>
    <input type="submit" value="dengl"/>
    </form>
</body>
</html>

需要把 javax.servlet. servlet-api.jar 放到lib下。 

记得把Java build path - source 的 output folder 设置为 项目名/webapp/WEB-INF/classes下

posted on 2015-03-17 10:08  远方的人  阅读(408)  评论(0编辑  收藏  举报

导航