ServletContextListener使用
@WebListener
public class MyServletContextListener implements ServletContextListener,
HttpSessionListener, HttpSessionAttributeListener {
// -------------------------------------------------------
// ServletContextListener implementation
// -------------------------------------------------------
@Override
public void contextInitialized(ServletContextEvent sce) {
/* This method is called when the servlet context is
initialized(when the Web application is deployed).
You can initialize servlet context related data here.
*/
System.out.println("context init");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
/* This method is invoked when the Servlet Context
(the Web application) is undeployed or
Application Server shuts down.
*/
System.out.println("context destroy");
}
}
结果:


本文介绍了一个具体的ServletContextListener实现示例,展示了如何通过监听器在Web应用启动和关闭时执行特定的操作。具体包括contextInitialized和contextDestroyed两个方法的具体实现。
853





