实现一个ServletContextListenervoid contextDestroyed(ServletContextEvent sce) Notification that the servlet context is about to be shut down. void contextInitialized(ServletContextEvent sce) Notification that the web application initialization process is starting.
能详细点吗? 不明白在说什么,listener是有什么用? 如何知道web程序被停止?
Notification that the servlet context is about to be shut down. void contextInitialized(ServletContextEvent sce)
Notification that the web application initialization process is starting.
ServletContextListener是监听容器的
例如:
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;public class LifeCycleListener implements ServletContextListener{ public void contextInitialized(ServletContextEvent event) {
//容器启动时执行的代码
}
public void contextDestroyed(ServletContextEvent event) {
//容器关闭时时执行的代码
}
}
但是非正常关闭(非stop();)容器是不会执行contextDestroyed 中代码的
顶17楼,写完这个类后,再在web.xml文件里配置一下。
像这样
<listener>
<listener-class>
xxx.xxx.LifeCycleListener
</listener-class>
</listener>
init()
service()
destory()在destory()里面执行点代码就可以了..
<listener>
<listener-class>
org.qpyong.emp.listen.BackListen
</listener-class>
</listener>
如果这样,我在哪里执行stop? 如果必须要执行stop,似乎实现不了我的要求,因为我永远不知道tomcat什么时候会关闭.