简介
- 监听器:专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动。
- Servlet 监听器:Servlet 规范中定义的一种特殊类,它用于监听 web 应用程序中的 ServletContext, HttpSession 和 ServletRequest 等域对象的创建与销毁事件,以及监听这些域对象中的属性发生修改的事件。
Servlet 监听器的分类
- 按监听的事件类型 Servlet 监听器可分为如下三种类型
- 监听域对象自身的创建和销毁的事件监听器
- 监听域对象中的属性的增加和删除的事件监听器
- 监听绑定到 HttpSession 域中的某个对象的状态的事件监听器
编写 Servlet 监听器
- Servlet 规范为每种事件监听器都定义了相应的接口,开发人员编写的事件监听器程序只需实现这些接口,web 服务器根据用户编写的事件监听器所实现的接口把它注册到相应的被监听对象上
- 一些 Servlet 事件监听器需要在 web 应用程序的 web.xml 文件中进行注册,一个 web.xml 文件中可以注册多个 Servlet 事件监听器,web 服务器按照它们在 web.xml 文件中的注册顺序来加载和注册这些 Serlvet 事件监听器。
- Serlvet 事件监听器的注册和调用过程都是由 web 容器自动完成的,当发生被监听的对象被创建,修改或销毁事件时,web容器将调用与之相关的 Servlet 事件监听器对象的相关方法,开发人员在在这些方法中编写的事件处理代码即被执行
- 由于一个 web 应用程序只会为每个事件监听器创建一个对象,有可能出现多个线程同时调用同一个事件监听器对象的情况,所以,在编写事件监听器类时,应考虑多线程安全的问题
监听域对象的创建和销毁
- 域对象创建和销毁的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象的创建和销毁事件的监听器。
- 域对象的创建和销毁时机
ServletContextListener
- ServletContextListener 接口用于监听 ServletContext 对象的创建和销毁事件。
- 当 ServletContext 对象被创建时,激发contextInitialized (ServletContextEvent sce)方法
- 当 ServletContext 对象被销毁时,激发contextDestroyed(ServletContextEvent sce)方法
HttpSessionListener
- HttpSessionListener 接口用于监听HttpSession对象的创建和销毁
- 创建一个Session时,激发sessionCreated(HttpSessionEvent se) 方法
- 销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se) 方法。
ServletRequestListener
- ServletRequestListener 接口用于监听ServletRequest 对象的创建和销毁
- 创建一个ServletRequest 对象时,激发requestInitialized(ServletRequestEvent sre)方法
- 销毁一个Session时,激发requestDestroyed(ServletRequestEvent sre)方法。
域对象中属性的变更的事件监听器
- 域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信息事件的监听器。
- 这三个监听器接口分别是
- ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener,这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同
attributeAdded 方法
- 当向被监听器对象中增加一个属性时,web容器就调用事件监听器的 attributeAdded 方法进行相应,这个方法接受一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象
- 各个域属性监听器中的完整语法定义为
public void attributeAdded(ServletContextAttributeEvent scae) public void attributeReplaced(HttpSessionBindingEvent hsbe) public void attributeRmoved(ServletRequestAttributeEvent srae)
attributeRemoved 方法
- 当删除被监听对象中的一个属性时,web 容器调用事件监听器的这个方法进行相应
- 各个域属性监听器中的完整语法定义为
public void attributeRemoved(ServletContextAttributeEvent scae) public void attributeRemoved (HttpSessionBindingEvent hsbe) public void attributeRemoved (ServletRequestAttributeEvent srae)
attributeReplaced 方法
- 当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的这个方法进行相应
- 各个域属性监听器中的完整语法定义为
public void attributeReplaced(ServletContextAttributeEvent scae) public void attributeReplaced (HttpSessionBindingEvent hsbe) public void attributeReplaced (ServletRequestAttributeEvent srae)
感知 Session 绑定的事件监听器
- 保存在 Session 域中的对象可以有多种状态:绑定到 Session 中;从 Session 域中解除绑定;随 Session 对象持久化到一个存储设备中;随 Session 对象从一个存储设备中恢复
- Servlet 规范中定义了两个特殊的监听器接口来帮助 JavaBean 对象了解自己在 Session 域中的这些状态:HttpSessionBindingListener接口和HttpSessionActivationListener接口 ,实现这两个接口的类不需要 web.xml 文件中进行注册
HttpSessionBindingListener接口
- 实现了HttpSessionBindingListener接口的 JavaBean 对象可以感知自己被绑定到 Session 中和从 Session 中删除的事件
- 当对象被绑定到 HttpSession 对象中时,web 服务器调用该对象的 void valueBound(HttpSessionBindingEvent event) 方法
- 当对象从 HttpSession 对象中解除绑定时,web 服务器调用该对象的 void valueUnbound(HttpSessionBindingEvent event)方法
HttpSessionActivationListener接口
- 实现了HttpSessionBindingListener接口的 JavaBean 对象可以感知自己被活化和钝化的事件
- 当绑定到 HttpSession 对象中的对象将要随 HttpSession 对象被钝化之前,web 服务器调用该对象的 void sessionWillPassivate(HttpSessionBindingEvent event) 方法
- 当绑定到 HttpSession 对象中的对象将要随 HttpSession 对象被活化之后,web 服务器调用该对象的 void sessionDidActive(HttpSessionBindingEvent event)方法