1. 先创建servlet ,然后立即调用init( )方法
2. 每次请求的时候,去调用service( ),
3. 每次容器卸载干净项目之前,先调用destroy( ) 方法
destroy()方法是在GenericServlet这个类中的,源代码如下
/**
* Called by the servlet container to indicate to a servlet that the
* servlet is being taken out of service. See {@link Servlet#destroy}.
*
*
*/
public void destroy() {
}
由此可见,源代码中的destroy是在tomcat中移除servlet时调用的,代码本身没有实现其他功能。
在复写destroy()函数:
@Override
public void destroy() {
logger.info("destroy");
super.destroy();
}
在以下三种情况下会调用destroy方法
tomcat remove servlet ( 卸载 )的时候,先调用destroy,再卸载
tomcat stop server的时候,调用destroy
修改servlet类代码,导致这个类被重新编译加载,调用destroy方法