Listener
# Listener
监听器 所有监听器都是基于观察者设计模式的
可以对 对象的创建销毁 域对象中属性变化 会话相关内容进行监听
# 观察者模式
- 事件源:触发事件的对象
- 事件:触发的动作,封装了事件源
- 监听器:当事件源触发事件后,可以完成功能
# 监听对象
- ServletContextListener 用于监听 ServletContext 对象的创建和销毁
- contextInitialized (ServletContextEvent sce) 对象创建时执行该方法
- contextDestroyed (ServletContextEvent sce) 对象销毁时执行该方法
- HttpSessionListener 监听 HttpSession 对象的创建和销毁
- sessionCreated (HttpsessionEvent se) 对象创建时执行该方法
- sessionDestroyed (HttpsessionEvent se) 对象销毁时执行该方法
- ServletRequestListener 监听 ServletRequest 的创建和销毁
- requestInitialized (ServletRequestEvent sre) 对象创建时执行该方法
- requestDestroyed (ServletRequestEvent sre) 对象销毁时执行该方法
# 监听域对象属性
- ServletContextAttributeListener 监听 ServletContext 应用域中的属性变化
- attributeAdded (ServletContextAttributeEvent scae) 域中添加属性执行该方法
- attributeRemoved (ServletContextAttributeEvent scae) 域中移除属性执行该方法
- attributeReplaced (ServletContextAttributeEvent scae) 域中替换属性执行该方法
- HttpSessionAttributeListener 监听 HttpSession 会话域中的属性变化
- attributeAdded (HttpSessionBindingEvent se) 域中添加属性执行该方法
- attributeRemoved (HttpSessionBindingEvent se) 域中移除属性执行该方法
- attributeReplaced (HttpSessionBindingEvent se) 域中替换属性执行该方法
- ServletRequestAttributeListener 监听 ServletRequest 请求域中的属性变化
- attributeAdded (ServletRequestAttributeEvent srae) 域中添加属性执行该方法
- attributeRemoved (ServletRequestAttributeEvent srae) 域中移除属性执行该方法
- attributeReplaced (ServletRequestAttributeEvent srae) 域中替换属性执行该方法
# 监听会话相关的感知型监听器
HttpSessionBindListener 感知对象和会话域绑定的监听器
- valueBound (HttpSessionBindEvent event) 数据添加到会话域中 (绑定) 执行方法
- valueUnbound (HttpSessionBindEvent event) 数据移除到会话域中 (解绑) 执行方法
HttpSessionActivationListener 感知会话域中对象钝化和活化的监听器
- seesionWillPassivate (HttpSessionEvent se) 会话域中数据钝化时执行该方法
- seesionDidActivate (HttpSessionEvent se) 会话域中数据活化时执行该方法
# 配置监听器
注解标识 @WebListener
- 拦截多个指定页面 @WebListener (value = {"/xxx","/xxx"})
web.xml 配置
<listener> <listener-class>com.example.demo</listener-class> </listener>1
2
3
编辑 (opens new window)
上次更新: 2023/12/06, 01:31:48