實現Servlet的三種方式:一個實現,兩個繼承
/*==========================================
* servlet的執行過程:
* 1.創建servlet對象(Tomcat執行)
* 2.第一次訪問servlet時執行
* init()方法 該方法只執行一次
* service()方法
* 3.之后每訪問一次 就執行一次service()方法
* 4.關閉服務時調用destroy()方法 該方法只執行一次
*servlet的服務周期
*1.創建
*2.初始化
*3.服務
*4.銷毀
*
*/
第一種方法,實現Servlet接口
1 package com.xinqi.sevlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.Servlet; 6 import javax.servlet.ServletConfig; 7 import javax.servlet.ServletException; 8 import javax.servlet.ServletRequest; 9 import javax.servlet.ServletResponse; 10 11 public class testServlet1 implements Servlet { 12 13 /** 14 * 銷毀方法 15 */ 16 public void destroy() { 17 System.out.println("這是銷毀方法!"); 18 19 } 20 21 /** 22 * 初始化方法 23 */ 24 public void init(ServletConfig arg0) throws ServletException { 25 System.out.println("這是初始化方法!"); 26 } 27 28 /** 29 * 服務方法 30 */ 31 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { 32 33 System.out.println("這是服務方法!"); 34 35 } 36 37 38 //獲取servlet配置信息 39 public ServletConfig getServletConfig() { 40 41 return null; 42 } 43 44 //獲取servlet信息 45 public String getServletInfo() { 46 47 return null; 48 } 49 }
第二種方法:繼承GenericServlet抽象類
GenericServlet抽象類中只有一個抽象方法service()方法
public abstract class GenericServletextends java.lang.Objectimplements Servlet, ServletConfig, java.io.Serializable
1 package com.xinqi.sevlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.GenericServlet; 6 import javax.servlet.ServletException; 7 import javax.servlet.ServletRequest; 8 import javax.servlet.ServletResponse; 9 10 public class testServlet2 extends GenericServlet{ 11 12 @Override 13 public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { 14 15 System.out.println("這是服務方法!"); 16 } 17 18 }
第三種方式:繼承HttpServlet抽象類重寫doPut()和doPost()方法
public abstract class HttpServletextends GenericServletimplements java.io.Serializable
1 package com.xinqi.sevlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class testServlet3 extends HttpServlet{ 11 12 @Override 13 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 15 System.out.println("這是get方法提交"); 16 17 doGet(req, resp); 18 } 19 20 21 @Override 22 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 23 System.out.println("這是post方法提交"); 24 } 25 26 }
配置文件的信息:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <display-name>servletPro</display-name> 4 5 <!-- sevlet配置 --> 6 <!-- 1.servlet的定義:告訴容器如何來創建servlet --> 7 <servlet> 8 <servlet-name>test1</servlet-name> 9 <servlet-class>com.xinqi.sevlet.testServlet1</servlet-class> 10 </servlet> 11 12 <!-- 2.配置servlet路徑 servlet映射 --> 13 <!-- url-pattern 必須以/開頭 訪問:http://localhost:8080/servletPro/login --> 14 <servlet-mapping> 15 <servlet-name>test1</servlet-name> 16 <url-pattern>/login</url-pattern> 17 </servlet-mapping> 18 19 20 <servlet> 21 <servlet-name>test2</servlet-name> 22 <servlet-class>com.xinqi.sevlet.testServlet2</servlet-class> 23 </servlet> 24 <servlet-mapping> 25 <servlet-name>test2</servlet-name> 26 <url-pattern>/login</url-pattern> 27 </servlet-mapping> 28 29 30 <servlet> 31 <servlet-name>test3</servlet-name> 32 <servlet-class>com.xinqi.sevlet.testServlet3</servlet-class> 33 </servlet> 34 <servlet-mapping> 35 <servlet-name>test3</servlet-name> 36 <url-pattern>/login</url-pattern> 37 </servlet-mapping> 38 39 </web-app>