天天看点

Java精选笔记_Servlet事件监听器

Servlet事件监听器

概述

在程序开发中,经常需要对某些事件进行监听,如监听鼠标点击事件、监听键盘按下事件等,此时就需要使用事件监听器。

事件监听器用于对程序中发生的事件进行监听,在监听的过程中会涉及几个重要组成部分:

事件(Event)

用户的一个操作,如点击一个按钮、调用一个方法、创建一个对象等。

事件源

产生事件的对象。

事件监听器(Listener)

负责监听发生在事件源上的事件。

事件处理器

监听器的成员方法,当事件发生的时候会触发对应的处理器(成员方法)。

Servlet事件监听器是一个实现特定接口的Java程序,专门用于监听Web应用程序,根据监听事件的不同可将这些接口分为三类

用于监听域对象创建和销毁的事件监听器

ServletContextListener接口

HttpSessionListener接口

ServletRequestListener接口

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

ServletContextAttributeListener接口HttpSessionAttributeListener接口ServletRequestAttributeListener接口

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

HttpSessionBindingListener接口、HttpSessionActivationListener接口

事件监听器工作步骤

(1)注册监听器

将监听器绑定到事件源,也就是注册监听器。

(2)传递事件对象

事件发生时会触发监听器的成员方法,即事件处理器,传递事件对象。

(3)处理时间源

事件处理器通过事件对象获得事件源,并对事件源进行处理。

监听域对象的生命周期

在Web应用程序的运行期间,Web容器会创建和销毁三个比较重要的对象ServletContext、HttpSession和ServletRequest,这些对象被称为域对象。

为了监听这些域对象的的生命周期,Servlet API中专门提供三个接口ServletContextListener、HttpSessionListener、ServletRequestListener,它们分别用于监听ServletContext对象的生命周期、监听HttpSession对象的生命周期、监听ServletRequest对象的生命周期。

ServletContextListener接口

该接口中共定义了两个事件处理方法

contextInitialized()方法

public void contextInitialized(servletContextEvent sce)

contextDestroyed()方法

public void contextDestroyed(servletContextEvent sce)

HttpSessionListener接口

该接口中共定义了两个事件处理方法

sessionCreated()方法

public void sessionCreated(HttpSessionEvent se)

sessionDestroyed()方法

public void sessionDestroyed(HttpSessionEvent se)

ServletRequestListener接口

该接口中定义了两个事件处理方法

requestInitialized()方法

public void requestInitialized(ServletRequestEvent sre)

requestDestroyed()方法

public void requestDestroyed(ServletRequestEvent sre)

监听域对象中的属性变更

监听域对象中的属性变更

提供的接口ServletContextAttributeListener、HttpSessionAttributeListener和ServletRequestAttributeListener接口。

监听域对象属性变更的接口

在程序开发中,不仅需要对域对象进行监听,有时还需要对某个域对象属性的变更进行监听,监听域对象属性变更的三个接口都定义了相同名称的方法,分别用于处理被监听对象属性的增加、删除和替换。

attributeAdded()方法

当向被监听的域对象中增加一个属性时,Web容器就调用事件监听器的attributeAdded()方法进行响应,该方法接收一个事件类型的参数。

public void attributeAdded(ServletContextAttributeEvent scab)

    上述是ServletContextAttributeListener接口中定义的方法,当向ServletContext对象中增加一个属性时,Web容器就调用这个方法并传递一个ServletContextEvent类型的参数

public void attributeAdded(HttpSessionBindindEvent se)

  上述是HttpSessionAttributeListener接口中定义的方法,当向HttpSession对象中增加一个属性时,Web容器就调用这个方法并传递一个HttpSessionBindindEvent类型的参数。

public void attributeAdded(ServletRequestAttributeEvent srae)

  上述是ServletRequestAttributeListener接口中定义的方法,当向ServletRequest对象中增加一个属性时,Web容器就调用这个方法并传递一个ServletRequestAttributeEvent类型的参数。

attributeRemoved()方法

当删除被监听对象中的一个属性时,Web容器调用事件监听器的attributeRemoved()方法进行响应。

public void attributeRemoved(ServletContextAttributeEvent scab)

public void attributeRemoved(HttpSessionBindindEvent se)

public void attributeRemoved(ServletRequestAttributeEvent srae)

这些方法接收的参数类型与上面讲解的attributeAdded()方法一样,监听器可以通过这个参数来获取正在删除属性的域对象。

attributeReplaced()方法

当被监听器的域对象中的某个属性被替换时,Web容器会调用事件监听器的attributeReplaced ()方法进行响应。

public void attributeReplaced(ServletContextAttributeEvent scab)

public void attributeReplaced(HttpSessionBindindEvent se)

public void attributeReplaced(ServletRequestAttributeEvent srae)

这些方法接收的参数类型与上面讲解的attributeAdded()方法一样,监听器可以通过这个参数来获取正在替换属性的域对象。

感知被HttpSession绑定的事件监听器

程序开发中经常使用Session域来存储对象,每个对象在该域中都有多种状态,如绑定(保存)到Session域中,从Session域中解除绑定、随Session对象持久化到一个存储设备中(钝化),随Session对象从一个存储设备中恢复(活化)。

为了观察Session域中对象的状态,Servlet API还提供了两个特殊的监听器接口 HttpSessionBindingListener和HttpSessionActivationListener,这两个接口专门用于监听JavaBean对象在Session域中的状态。 

HttpSessionBindingListener接口

在使用JavaBean对象时经常会判断该对象是否绑定到Session域中,该接口用于监听JavaBean对象绑定到HttpSession对象和从HttpSession对象解绑的事件。

接口中共定义了两个事件处理方法

valueBound()方法

public void valueBound(HttpSessionBindingEvent event) 

valueUnbound()方法

public void valueUnbound(HttpSessionBindingEvent event) 

HttpSessionActivationListener接口

为了监听HttpSession中对象活化和钝化的过程,Servlet API专门提供了HttpSessionActivationListener接口

该接口定义了两个事件处理方法

sessionWillPassivate()方法

public void sessionWillPassivate(HttpSessionEvent se) 

sessionDidActivate()方法

public void sessionDidActivate(HttpSessionEvent se) 

分类

上下文监听器

    ServletContextListener

    contextInitialized(ServletContextEvent)

    contextDestroyed(ServletContextEvent)

    ServletContextAttributeListener

    attributeAdded(ServletContextAttributeEvent)

    attributeRemoved(ServletContextAttributeEvent)

    attributeReplaced(ServletContextAttributeEvent)

会话监听器

    HttpSessionListener

    sessionCreated(HttpSessionEvent)

    sessionDestroyed(HttpSessionEvent)

    HttpSessionAttributeListener

    attributeAdded(HttpSessionBindingEvent)

    attributeRemoved(HttpSessionBindingEvent)

    attributeReplaced(HttpSessionBindingEvent)

    HttpSessionActivationListener

    sessionWillPassivate(HttpSessionEvent)

    sessionDidActivate(HttpSessionEvent)

    HttpSessionBindingListener

    valueBound(HttpSessionBindingEvent)

    valueUnbound(HttpSessionBindingEvent)

请求监听器

    ServletRequestListener

    requestDestroyed(ServletRequestEvent)

    requestInitialized(ServletRequestEvent)

    ServletRequestAttributeListener

    attributeAdded(ServletRequestAttributeEvent)

    attributeRemoved(ServletRequestAttributeEvent)

    attributeReplaced(ServletRequestAttributeEvent)

配置

注解