一、 Listener 按照监听的对象分类共有六种
- ServletContext 对象生命周期监听器与属性操作监听器;
- HttpSession 对象生命周期监听器与属性操作监听器;
- ServletRequest 对象生命周期监听器与属性操作监听器;
二、细分
1.ServletContextListener
(1).ServletContext 对象生命周期监听器
ServletContextListener 接口定义了 ServletContext 对象生命周期的监听行为
- void contextInitialized(ServletContextEvent sce)
Servlet 对象创建后会触发该监听方法,并将 ServletContext 对象传递到该方法中。
- void contextDestroyed(ServletContextEvent sce)
ServletContext 对象销毁之前会触发该监听方法,并将 ServletContext 对象传递到该方法中。
(2).ServletContext 对象的属性操作监听器
ServletContextListener 接口定义了 ServletContext 对象生命周期的监听行为
- void attributeAdded(ServletContextAttributeEvent scae)
向 ServletContext 对象中添加属性时会触发该监听方法,并将 ServletContext 对象传递到 该方法中。
触发事件的方法 servletContext.setAttribute(“key”,“value”)
- void attributeRemoved(ServletContextAttributeEvent scae)
当从 ServletContext 对象中删除属性时会触发该监听方法,并将 ServletContext 对象传递 到该方法中。
触发事件方法 servletContext.removeAttribute(“key”)
- void attributeReplaced(ServletContextAttributeEvent scae)
当从 ServletContext 对象中属性的值发生替换时会触发该监听方法,并将 ServletContext 对象传递到该方法中。
触发事件的方法 servletContext.setAttribute(“key”,“value”)
2.HttpSessionListener
(1).HttpSession 对象生命周期监听器
HttpSessionListener 接口定义了 HttpSession 对象生命周期的监听行为。
- void sessionCreated(HttpSessionEvent se)
HttpSession 对象创建后会触发该监听方法,并将已创建 HttpSession 对象传递到该方法57中
- void sessionDestroyed(HttpSessionEvent se)
HttpSession 对象在销毁之前会触发该监听方法,并将要销毁的HttpSession 对象传递到该方法中。
(2).HttpSession 对象的属性操作监听器
HttpSessionAttributeListener 接口定义了对于 HttpSession 对象属性操作的监听行为。
- void attributeAdded(HttpSessionBindingEvent se)
向 HttpSession 对象中添加属性时会触发该监听方法,并将 HttpSession 对象传递到该方 法中。触发事件的方法HttpSession.setAttribute(“key”,“value”)。
触发事件的方法 servletContext.setAttribute(“key”,“value”)
- void attributeRemoved(HttpSessionBindingEvent se)
当从 HttpSession 对象中删除属性时会触发该监听方法,并将 HttpSession 对象传递到该方法中。触发事件方法 HttpSession.removeAttribute(“key”)
- void attributeReplaced(HttpSessionBindingEvent se)
当从 HttpSession 对象中属性的值发生替换时会触发该监听方法,并将 HttpSession 对象 传递到该方法中。触发事件的方法HttpSession.setAttribute(“key”,“value”)
3.HttpRequestListener
(1).HttpServletRequest 对象的生命周期监听器
ServletRequestListener 接口定义了 ServletReqest(是 HttpServletRequest 接口的父接口类型)对象生命周期的监听行为。
- void requestInitialized(ServletRequestEvent sre)
HttpSession 对象创建后会触发该监听方法,并将已创建 HttpSession 对象传递到该方法57中
- void requestDestroyed(ServletRequestEvent sre)
HttpServletRequest 对象在销毁之前会触发该监听方法,并将要销毁的 ServletRequest 对 象传递到该方法中。
(2).HttpServletRequest 对象的属性操作监听器
ServletRequestAttributeListener 接口定义了对于 HttpServletRequest 对象属性操作的监听行为
- void attributeAdded(ServletRequestAttributeEvent srae)
向 HttpServletRequest 对象中添加属性时会触发该监听方法,并将 ServletRequest 对象传 递到该方法中。
触发事件的方法 HttpServletRequest.setAttribute(“key”,“value”)
- void attributeRemoved(ServletRequestAttributeEvent srae)
当从 HttpServletRequest 对象中删除属性时会触发该监听方法,并将 ServletRequest 对象传递到该方法中。
触发事件方法 HttpServletRequest.removeAttribute(“key”)
- void attributeReplaced(ServletRequestAttributeEvent srae)
当 从 HttpServletRequest 对 象 中 属 性 的 值 发 生 替 换 时 会 触 发 该 监 听 方 法 , 并将 ServletRequest 对象传递到该方法中。
触发事件的方法HttpServletRequest.setAttribute(“key”,“value”)