Java开发之Servlet生命周期
Servlet会在服务器启动或第一次请求该Servlet的时候开始生命周期,在服务器结束的时候结束生命周期。无论请求多少次Servlet,最多只有一个Servlet实例。多个客户端并发请求Servlet时,服务器会启动多个线程分别执行该Servlet的service()方法。
一、重点方法说明
1.init(ServletConfig conf)
加载Servlet时运行此方法,如果load-on-startup为0,则客户端第一次请求该Servlet时加载Servlet,如果load-on-startup配置为1,则容器启动时加载Servlet。
2.Service(ServletRequest req, ServletResponse res)
客户端每次请求Servlet都会运行该方法,该方法判断访问类型,然后根据HttpServletRequest的getMethod()返回结果判断是执行doGet还是doPost,doPut等。
3.destroy()
卸载Servlet时执行该方法,当容器关闭时会先卸载所有的Servlet,因此容器关闭时会执行destroy方法。
二、例子实践
1.web.xml
1 <!-- Servlet生命周期演示 --> 2 <servlet> 3 <servlet-name>LifeCycleServlet</servlet-name> 4 <servlet-class>com.servlet.LifeCycleServlet</servlet-class> 5 </servlet> 6 <servlet-mapping> 7 <servlet-name>LifeCycleServlet</servlet-name> 8 <url-pattern>/servlet/LifeCycleServlet</url-pattern> 9 </servlet-mapping>
2.LifeCycleServlet
1 package com.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletConfig; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class LifeCycleServlet extends HttpServlet { 13 14 private double startPoint; 15 16 /** 17 * Destruction of the servlet. <br> 18 */ 19 public void destroy() { 20 this.log("执行destroy()方法..."); 21 // Put your code here 22 } 23 24 public void doGet(HttpServletRequest request, HttpServletResponse response) 25 throws ServletException, IOException { 26 this.log("执行doGet()方法..."); 27 response.setContentType("text/html"); 28 response.setCharacterEncoding("UTF-8"); 29 PrintWriter out = response.getWriter(); 30 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 31 out.println("<HTML>"); 32 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 33 out.println(" <BODY>"); 34 out.println("<form method='post'>"); 35 out.println("<div align='center'>"); 36 out.print("<h1>Servlet 生命周期(1)</h1></br>"); 37 out.println("<input type='submit' value='点我'>"); 38 out.println("</div>"); 39 out.println("</form>"); 40 out.println(" </BODY>"); 41 out.println("</HTML>"); 42 out.flush(); 43 out.close(); 44 45 } 46 47 public void doPost(HttpServletRequest request, HttpServletResponse response) 48 throws ServletException, IOException { 49 this.log("执行doPost()方法..."); 50 response.setContentType("text/html"); 51 response.setCharacterEncoding("UTF-8"); 52 PrintWriter out = response.getWriter(); 53 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 54 out.println("<HTML>"); 55 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 56 out.println(" <BODY>"); 57 out.println("<div align='center'>"); 58 out.print("<h1>Servlet 生命周期(2)</h1></br>"); 59 out.println("<input type='button' onclick='history.go(-1)' value='返回上一级'>"); 60 out.println("</div>"); 61 out.println(" </BODY>"); 62 out.println("</HTML>"); 63 out.flush(); 64 out.close(); 65 66 } 67 68 public void service(HttpServletRequest request,HttpServletResponse response) 69 throws ServletException, IOException { 70 this.log("执行service()方法..."); 71 super.service(request, response); 72 } 73 74 public void init() throws ServletException { 75 // Put your code here 76 this.log("执行init()方法..."); 77 } 78 79 }
3.效果图
浏览器:
控制台输出: