监听器

1实现特定接口的普通java程序

2专门用于监听另一个对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行。

原理图

 

Listener监听器

 

事件源划分

ServletContext, HttpSession 和 ServletRequest 这三个域对象。

事件源对应的监听器

监听三个域对象创建和销毁的事件监听器

 

ServletContextListener 接口用于监听 ServletContext 对象的创建和销毁事件。

当 ServletContext 对象被创建时,激发contextInitialized (ServletContextEvent sce)方法

当 ServletContext 对象被销毁时,激发contextDestroyed(ServletContextEvent sce)方法

HttpSessionListener接口用于监听HttpSession的创建和销毁

创建一个Session时,sessionCreated(HttpSessionEvent se) 方法将会被调用。

销毁一个Session时,sessionDestroyed (HttpSessionEvent se) 方法将会被调用。销毁HttpSession不精确。

ServletRequestListener 接口用于监听ServletRequest 对象的创建和销毁。

Request 对象被创建时,监听器的requestInitialized方法将会被调用。

Request对象被销毁时,监听器的requestDestroyed方法将会被调用。

监听域对象中属性的增加和删除的事件监听器

对应的接口

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)

监听绑定到HttpSession 域中的某个对象的状态的事件监听器

Servlet 规范中定义了两个特殊的监听器接口来帮助 JavaBean 对象了解自己在Session 域中的这些状态,

HttpSessionBindingListener和HttpSessionActivationListener接口 ,实现这两个接口的类不需要 web.xml 文件中进行注册,因为事件源和监听器都是JavaBean自身。

 

HttpSessionBindingListener接口

当对象被绑定到 HttpSession 对象中时:web 服务器调用该对象的  void valueBound(HttpSessionBindingEvent event) 方法

当对象从 HttpSession 对象中解除绑定时:web 服务器调用该对象的 void valueUnbound(HttpSessionBindingEvent event)方法

HttpSessionActivationListener接口

sessionWillPassivate(HttpSessionBindingEventevent)方法 

当绑定到HttpSession对象中的对象将要随HttpSession对象被钝化之前,web服务器调用

void  sessionDidActive(HttpSessionBindingEvent event)方法

当绑定到 HttpSession 对象中的对象将要随 HttpSession 对象被活化之后,web 服务器调用

 

Servlet 监听器

一个 web.xml 文件中可以配置多个 Servlet 事件监听器,web 服务器按照它们在 web.xml 文件中的注册顺序来加载和注册这些 Serlvet 事件监听器。

 

 

    
监听器的工作过程和生命周期


  开发过程:
1写一个普通类实现对应的接口,即事件监听器
2在web.xml文件中注册事件监听器
    <!-- 事件源注册事件监听器,由容器完成 -->
    <listener>
        <listener-class>cn.itcast.web.listener.MyServletContextListener</listener-class>        
      </listener> 

   生命周期:
    空参构造(1次)->初始化(1次)->销毁化(1次),是一个单例的模式
     
   在部署web应用是产生,即用户第一次访问之前已经产生,在重新部署web应用时,后销毁原监听器,再产生新的监听器

 

 

案例

监听器[用于监听HttpSesison产生和销毁

web.xml

      <listener>
        <listener-class>cn.xxx.web.listener.MyHttpSessionListener</listener-class>
    </listener>  

 

代码

//监听器[用于监听HttpSesison产生和销毁]
public class MyHttpSessionListener implements HttpSessionListener {
    //产生
    public void sessionCreated(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        System.out.println(session.getId());
        System.out.println("sessionCreated()" + new Date().toLocaleString());
    }
    //销毁
    public void sessionDestroyed(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        System.out.println(session.getId());
        System.out.println("sessionDestroyed()" + new Date().toLocaleString());
    }
}

 

测试:客户端发起一个server请求

54DF50C7F845D62660348E129401A2FD
sessionCreated()2018-9-26 19:53:43

 

相关文章: