package com.cl.servlet;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** service方法、doGet方法和doPost方法的區別* service方法:可以處理get/post方式的請求,如果servlet中有service方法,會優先調用service方法* doGet方法: 處理get方法的請求* doPost方法: 處理post方式的請求* 注意:如果在覆寫的service方法中調用了父類的service方法 super.service(arg0, arg1);* 則service方法處理完后,會再次根據請求方式響應的doGet和doPost方法執行* 所以,一般情況下,是不在覆寫的service中調用父類的service方法的,避免出現405錯誤* @author Administrator**/
public class ServletMethod extends HttpServlet {@Overrideprotected void service(HttpServletRequest arg0, HttpServletResponse arg1)throws ServletException, IOException {System.out.println("我是service");super.service(arg0, arg1);}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {System.out.println("我是doGet方法");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {System.out.println("我是doPost方法");}
}
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>02-MyServlet</display-name><servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>servletLife</servlet-name><servlet-class>com.cl.servlet.servletLife</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>ServletMethod</servlet-name><servlet-class>com.cl.servlet.ServletMethod</servlet-class></servlet><servlet-mapping><servlet-name>servletLife</servlet-name><url-pattern>/life</url-pattern></servlet-mapping><servlet-mapping><servlet-name>ServletMethod</servlet-name><url-pattern>/method</url-pattern></servlet-mapping><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>
</web-app>