Java 中 Listener监听器作用
1.1 Listener对象分类
Servlet的Listener监听器包括三种类型:
1、ServletContextListener 监听ServletContext对象
- public void contextInitialized(ServletContextEvent event);
- public void contextDestoryed(ServletContextEvent event);
- public void contextInitialized(ServletContextEvent event);
- public void contextDestoryed(ServletContextEvent event);
2、HttpSessionListener 监听Session对象
- public void sessionCreated(HttpSessionEvent event);
- public void sessionDestoryed(HttpSessionEvent event);
- public void sessionCreated(HttpSessionEvent event);
- public void sessionDestoryed(HttpSessionEvent event);
1、 HttpRequestListener 监听Request对象
- public void requestinitialized(ServletRequestEvent event);
- public void requestDestoryed(ServletRequestEvent event);
- public void requestinitialized(ServletRequestEvent event);
- public void requestDestoryed(ServletRequestEvent event);
Web.xml文件配置
- <listener>
- <listener-class>listener.MyListener</listener-class>
- </listener>
- <listener>
- <listener-class>listener.MyListener</listener-class>
- </listener>
1.2 Listener按属性分类
按照监听事件类型划分分为如下类型:
1、 用于监听域对象自身的创建和销毁的事件监听器。
2、 用于监听域对象的属性的增加和删除的事件监听器。
3、 用于监听绑定到HttpSession域中的某个对象的状态的事件监听器。
1.2.1 监听对象的属性事件监听器接口
1、 attributeAdd当被监听域对象中增加属性时会调用该系列监听器
- public void attributeAdded(ServletContextAttributeEvent event);
- public void attributeAdded(HttpSessionBindingEvent event);
- public void attributeAdded(ServletRequestAttributeEvent event);
- public void attributeAdded(ServletContextAttributeEvent event);
- public void attributeAdded(HttpSessionBindingEvent event);
- public void attributeAdded(ServletRequestAttributeEvent event);
2、 attributeRemoved当被监听域对象中删除属性时会调用该系列监听器
- public void attributeRemoved(ServletContextAttributeEvent event);
- public void attributeRemoved(HttpSessionBindingEvent event);
- public void attributeRemoved(ServletRequestAttributeEvent event);
- public void attributeRemoved(ServletContextAttributeEvent event);
- public void attributeRemoved(HttpSessionBindingEvent event);
- public void attributeRemoved(ServletRequestAttributeEvent event);
3、 attributeReplaced当被监听域对象中属性变更时时会调用该系列监听器
- public void attributeReplaced(ServletContextAttributeEvent event);
- public void attributeReplaced(HttpSessionBindingEvent event);
- public void attributeReplaced(ServletRequestAttributeEvent event);
- public void attributeReplaced(ServletContextAttributeEvent event);
- public void attributeReplaced(HttpSessionBindingEvent event);
- public void attributeReplaced(ServletRequestAttributeEvent event);
注:使用属性监听器需要继承如下接口,实现以上方法
- ServletContextAttributeListener,
- HttpSessionAttributeListener,
- ServletRequestAttributeListener
- ServletContextAttributeListener,
- HttpSessionAttributeListener,
- ServletRequestAttributeListener
1.2.2 感知Session绑定的事件监听器
保存到Session域中的对象可以有多种状态:
1、 绑定到Session域中
2、 从Session域中解除绑定
3、 Session对象持久化到存储设备
4、 Session对象从一个存储设备中恢复
可以使用
- HttpSessionBindingListener、HttpSessionActivationListener
- HttpSessionBindingListener、HttpSessionActivationListener
两个监听接口实现JavaBean对象的绑定,从而了解JavaBean对象在Session域的状态。
- HttpSessionBindingListener
- public void valueBound(HttpSessionBindingEvent event);
- public void valueUnbound(HttpSessionBindingEvent event);
- HttpSessionActivationListener
- public void sessionWillPassivate(HttpSessionEvent event);
- HttpSessionBindingListener
- public void valueBound(HttpSessionBindingEvent event);
- public void valueUnbound(HttpSessionBindingEvent event);
- HttpSessionActivationListener
- public void sessionWillPassivate(HttpSessionEvent event);
注:当Session对象持久化到文件系统时,激活上面方法。
- public void sessionDidActivate(HttpSessionEvent event);
- public void sessionDidActivate(HttpSessionEvent event);
注:当Session对象从文件系统恢复时,激活上面方法。