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.效果圖
浏覽器:
控制台輸出: