在ServletContext创建和关闭时,都会通知ServletContextListener监听器
常用方法如下:
方法声明
功能介绍
void contextInitialized(ServletContextEvent sce)
当ServletContext创建的时候,将会调用这个方法
void contextDestroyed(ServletContextEvent sce)
当ServletContext销毁的时候(例如关闭应用服务器 或者重新加载应用),将会调用这个方法
例子:
效果:
启动服务器时:
停止服务器时:
向ServletContext添加、删除或者替换一个属性的时候,将会通知
ServletContextAttributesListener监听器
void attributeAdded(ServletContextAttributeEvent scae)
往ServletContext中加入一个属性的时 候触发
void attributeRemoved(ServletContextAttributeEvent scae)
从ServletContext中删除一个属性的时 候触发
void attributeReplaced(ServletContextAttributeEvent scae)
改变ServletContext中属性的时候触发
启动服务器,访问contextAttribute.jsp: