①什么是Servlet监听器?
其主要功能就是监听Web的各种操作,当相关的操作促发后讲产生的时间,并对此事件进行处理。
②监听器接口:
ServletContextListener接口用于监听Web应用程序启动和销毁的事件。
contextInitialized(ServletContextEventsce):通知正在接受的对象,应用程序已经被加载及初始化
contextDestroyed(ServletContextEventsce):通知正在接受的对象,应用程序已经被销毁
ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性。
当创建ServletContext时,激发contextInitialized (ServletContextEvent sce)方法;
当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
HttpSessionBindingListener 接口有两个方法
voidvalueBound(HttpSessionBindingEvent event):当对象正在绑定到Session中,Servlet容器调用该方法来通知该对象
voidvalueUnbound(HttpSessionBindingEvent event):当从Session中删除对象时,Servlet容器调用该方法来通知该对象
HttpSessionAttributeListener 接口
监听HttpSession中的属性的操作
当在Session中增加一个属性时,激发
attributeAdded(HttpSessionBindingEventse)方法;
当在Session删除一个属性时,激发
attributeRemoved(HttpSessionBindingEventse)方法;
当在Session属性被重新设置时,激发
attributeReplaced(HttpSessionBindingEventse)方法。
HttpSessionListener 接口
监听HttpSession对象的创建和销毁操作
当创建一个Session时,激发
sessionCreated(HttpSessionEvent se)方法
当销毁一个Session时,激发
sessionDestroyed(HttpSessionEvent se)方法
监听请求生命周期内事件
请求作用域范围内的生命周期事件用于管理整个request生命周期的状态和资源
ServletRequestListener接口
publicvoid requestDestroyed(ServletRequestEvent sre)
:当请求被销毁时被处理。
publicvoid requestInitialized(ServletRequestEvent sre)
:当请求被创建时被处理
ServletRequestAttributeListener接口
publicvoid attributeAdded(ServletRequestAttributeEvent arg0) :当在请求作用域中添加一个属性的时候调用该方法。
publicvoid attributeRemoved(ServletRequestAttributeEvent arg0) :当在请求作用域中删除一个属性时调用
publicvoid attributeReplaced(ServletRequestAttributeEvent arg0) :当在请求作用域中替换一个属性值的时候调用