Servlet的生命周期涉及3个问题: servlet类对象什么时候创建,什么时候调用什么方法,什么时候销毁。
以前的对象,其生命周期由程序开发者编写决定,例如:
Student stu = new Student();//创建Student对象
stu.study();//调用方法
stu = null;//告诉垃圾回收器GC,该对象可以进行回收了
Servlet程序的生命周期由tomcat服务器控制的!!!!
序号 | 方法 | 调用时机和次数 |
---|---|---|
1 | 构造方法 | 创建servlet对象的时候调用。默认情况下,第一次访问servlet的时候创建servlet对象只调用1次。servlet对象在tomcat是单实例的。 |
2 | init方法 | 创建完servlet对象的时候调用。只调用1次。 |
3 | service方法 | 每次发出请求时调用。调用n次。 |
4 | destroy方法 | 销毁servlet对象的时候调用。停止服务器或者重新部署web应用时销毁servlet对象。只调用1次。 |
案例:验证Servlet生命周期的4个重要方法的调用次数
LifeDemo.java
package com.rk.http.b_lifecycle; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; public class LifeDemo extends HttpServlet { //1、构造方法 public LifeDemo() { System.out.println("1、LifeDemo对象被创建!"); } //2、init方法 @Override public void init(ServletConfig config) throws ServletException { System.out.println("2、调用LifeDemo对象的init方法"); } //3、service方法 @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { System.out.println("3、调用LifeDemo对象的service方法"); } //4、destroy方法 @Override public void destroy() { System.out.println("4、LifeDemo对象销毁"); } }
web.xml配置
<servlet> <servlet-name>LifeDemo</servlet-name> <servlet-class>com.rk.http.b_lifecycle.LifeDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>LifeDemo</servlet-name> <url-pattern>/life</url-pattern> </servlet-mapping>
使用URL(http://localhost:8080/myweb/life)访问5次后,停止Tomcat,得到如下输出:
1、LifeDemo对象被创建! 2、调用LifeDemo对象的init方法 3、调用LifeDemo对象的service方法 3、调用LifeDemo对象的service方法 3、调用LifeDemo对象的service方法 3、调用LifeDemo对象的service方法 3、调用LifeDemo对象的service方法 五月 23, 2016 11:20:45 上午 org.apache.coyote.http11.Http11AprProtocol pause 信息: Pausing Coyote HTTP/1.1 on http-8080 五月 23, 2016 11:20:45 上午 org.apache.coyote.ajp.AjpAprProtocol pause 信息: Pausing Coyote AJP/1.3 on ajp-8009 五月 23, 2016 11:20:46 上午 org.apache.catalina.core.StandardService stop 信息: Stopping service Catalina 五月 23, 2016 11:20:46 上午 org.apache.catalina.core.ApplicationContext log 信息: SessionListener: contextDestroyed() 五月 23, 2016 11:20:46 上午 org.apache.catalina.core.ApplicationContext log 信息: ContextListener: contextDestroyed() 4、LifeDemo对象销毁 五月 23, 2016 11:20:46 上午 org.apache.coyote.http11.Http11AprProtocol destroy 信息: Stopping Coyote HTTP/1.1 on http-8080 五月 23, 2016 11:20:46 上午 org.apache.coyote.ajp.AjpAprProtocol destroy 信息: Stopping Coyote AJP/1.3 on ajp-8009
package javax.servlet; import java.io.IOException; /** * 对Servlet进行介绍 * A servlet is a small Java program that runs within a Web server. * Servlets receive and respond to requests from Web clients, * usually across HTTP, the HyperText Transfer Protocol. * * 实现Servlet接口的两种方法 * To implement this interface, you can write a generic servlet *that extends *<code>javax.servlet.GenericServlet</code> or an HTTP servlet that *extends <code>javax.servlet.http.HttpServlet</code>. * *Servlet接口中定义了一些方法,称为life-cycle methods *This interface defines methods to initialize a servlet, *to service requests, and to remove a servlet from the server. *These are known as life-cycle methods and are called in the *following sequence: * *life-cycle methods的调用顺序如下:The servlet is constructed-->init-->service-->destory *The servlet is constructed, then initialized with the <code>init</code> method. *Any calls from clients to the <code>service</code> method are handled. *The servlet is taken out of service, then destroyed with the *<code>destroy</code> method, then garbage collected and finalized. * *Servlet接口还提供了另外两个方法:getServletConfig和getServletInfo *In addition to the life-cycle methods, this interface *provides the <code>getServletConfig</code> method, which the servlet *can use to get any startup information, and the <code>getServletInfo</code> *method, which allows the servlet to return basic information about itself, *such as author, version, and copyright. * */ public class Servlet { /** * The servlet container calls the <code>init</code> * method exactly once after instantiating the servlet. * The <code>init</code> method must complete successfully * before the servlet can receive any requests. */ public void init(ServletConfig config) throws ServletException; /** * Called by the servlet container to allow the servlet to respond to a request. * This method is only called after the servlet‘s <code>init()</code> method has completed successfully. * * Servlets typically run inside multithreaded servlet containers * that can handle multiple requests concurrently. Developers must * be aware to synchronize access to any shared resources such as files, * network connections, and as well as the servlet‘s class and instance variables. */ public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException; /** * Called by the servlet container to indicate that the servlet is being taken out of service. * This method is only called once all threads within the servlet‘s * <code>service</code> method have exited or after a timeout period has passed. * After the servlet container calls this method, it will not call * the <code>service</code> method again on this servlet. * * This method gives the servlet an opportunity * to clean up any resources that are being held (for example, memory, * file handles, threads) and make sure that any persistent state is * synchronized with the servlet‘s current state in memory. */ public void destroy(); /** * Returns a <code>ServletConfig</code> object, which contains initialization and startup parameters for this servlet. * The <code>ServletConfig</code> object returned is the one passed to the <code>init</code> method. */ public ServletConfig getServletConfig(); /** * Returns information about the servlet, such as author, version, and copyright. */ public String getServletInfo(); }
Tomtcat内部代码运行:
1)通过映射找到到servlet-class的内容
字符串:com.rk.http.b_lifecycle.LifeDemo
2)通过反射构造LifeDemo对象
2.1 得到字节码对象
Class clazz = class.forName("com.rk.http.b_lifecycle.LifeDemo");
2.2 调用无参数的构造方法来构造对象
Object obj = clazz.newInstance(); ---1.servlet的构造方法被调用
3)创建ServletConfig对象,通过反射调用init方法
3.1 得到方法对象
Method m = clazz.getDeclareMethod("init",ServletConfig.class);
3.2 调用方法
m.invoke(obj,config); --2.servlet的init方法被调用
4)创建request,response对象,通过反射调用service方法
4.1 得到方法对象
Method m =clazz.getDeclareMethod("service",HttpServletRequest.class,HttpServletResponse.class);
4.2 调用方法
m.invoke(obj,request,response); --3.servlet的service方法被调用
5)当tomcat服务器停止或web应用重新部署,通过反射调用destroy方法
5.1 得到方法对象
Method m = clazz.getDeclareMethod("destroy",null);
5.2 调用方法
m.invoke(obj,null); --4.servlet的destroy方法被调用
原文地址:http://lsieun.blog.51cto.com/9210464/1782079