1.ServletContextListener:
用于监听应用程序的启动与销毁的事件,监听类需要实现javax.servlet.ServletContextListener接口.
ServletContextListener是ServletContext的监听者,如果ServletContext发生变化,如服务器启动时ServletContext被创建,服务器关闭时
ServletContext被销毁.
ServletContextListener 接口的方法:
>void contextInitialized(ServletContextEvent sce):通知正在接受的对象,应用程序已经被加载及初始化.
>void contextDestroyed(ServletContextEvent sce):通知正在接受的对象,应用程序已经被销毁.
ServletContextEvent中的方法:
>ServletContext getServletContext():取得ServletContext对象
配置文件:
<listener>
<listener-class>com.lovo.MyServletContextListener</listener-class>
</listener>
2.ServletContextAttributeListener:
用于监听web应用属性的改变的事件,包括:增加属性,修改属性,删除属性.监听需要实现javax.servlet.ServletContextsAttributeListener:
ServletContextsAttributeListener接口方法:
>void attributeAdded(ServletContextAttributeEvent scae):若有属性对象加入Application的范围,通知正在收听的对象.
>void AttributeRemoved(ServletContextAttributeEvent scae):若有属性对象从Application的范围移除,通知正在收听的对象.
>void AttributeReplaced(ServletContextAttributeEvent scae):若在Application的范围中,有对象替代了另一个对象时,通知正在收听的对象.
ServletContextEvent中的方法:
>java.lang.String.getName():回传属性的名称
>java.lang.Object.getValue():回传属性的值
监听会话范围内的事件:
3.HttpSessionBindingListener接口:对象绑定
注意:HttpSessionBindingListener接口是唯一不需要在web.xml中设定的Listener
4.HttpSessionAttributeListener接口:不用注册到web.xml
5.HttpSessionListener接口:在请求作用域范围内的生命周期事件用于管理整个request生命周期的状态和资源.
方法:
>void requestDestroyed(ServletRequestEvent e)’:当请求被销毁时被处理
>void requestInitialized(ServletRequestEvent e)’:当请求被创建时被处理
6. ServletRequestAttributeListener接口方法:
>void attributeAdded(ServletRequestAttributeEvent scae):若请求作用域添加一个属性的时候调用该方法;
>void AttributeRemoved(ServletRequestAttributeEvent scae):当请求作用域删除一个属性时调用该方法
>void AttributeReplaced(ServletRequestAttributeEvent scae):当请求作用域替换一个属性时候呀被调用