ServletContextListener是用来监听ServletContext(即Web应用程序)的生命周期的,当Servlet 容器启动或终止Web 应用时,会触发ServletContextEvent事件,该事件由ServletContextListener来处理。
因此在整个web应用中是无法获取到ServletContextListener对象的,如果你想控制web应用,那么只需要获取servletContext对象即可,在 ServletContextListener的实现类中定义ServletContext 的static(生命期为整个web应用程序)修饰的变量servletContext,然后定义处理ServletContextEvent 事件的contextInitialized方法:
public void contextInitialized(ServletContextEvent sce)
{
servletContext = sce.getServletContext(); //获取ServletContext对象(web应用)
}
因此在整个web应用中是无法获取到ServletContextListener对象的,如果你想控制web应用,那么只需要获取servletContext对象即可,在 ServletContextListener的实现类中定义ServletContext 的static(生命期为整个web应用程序)修饰的变量servletContext,然后定义处理ServletContextEvent 事件的contextInitialized方法:
public void contextInitialized(ServletContextEvent sce)
{
servletContext = sce.getServletContext(); //获取ServletContext对象(web应用)
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货