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
<!-- Servlet生命周期演示 -->
<servlet>
<servlet-name>LifeCycleServlet</servlet-name>
<servlet-class>com.servlet.LifeCycleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LifeCycleServlet</servlet-name>
<url-pattern>/servlet/LifeCycleServlet</url-pattern>
</servlet-mapping>
2.LifeCycleServlet
package com.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class LifeCycleServlet extends HttpServlet { private double startPoint; /**
* Destruction of the servlet. <br>
*/
public void destroy() {
this.log("执行destroy()方法...");
// Put your code here
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.log("执行doGet()方法...");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<form method='post'>");
out.println("<div align='center'>");
out.print("<h1>Servlet 生命周期(1)</h1></br>");
out.println("<input type='submit' value='点我'>");
out.println("</div>");
out.println("</form>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.log("执行doPost()方法...");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<div align='center'>");
out.print("<h1>Servlet 生命周期(2)</h1></br>");
out.println("<input type='button' onclick='history.go(-1)' value='返回上一级'>");
out.println("</div>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close(); } public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
this.log("执行service()方法...");
super.service(request, response);
} public void init() throws ServletException {
// Put your code here
this.log("执行init()方法...");
} }
3.效果图
浏览器:
控制台输出: