首页 > 代码库 > Servlet实现的三种方式

Servlet实现的三种方式

实现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     @Override13     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     @Override13     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     @Override22     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>

 

Servlet实现的三种方式