29. 事件监听

简介
  • 监听器:专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动。
  • 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)
实现步骤
  • 创建类实现监听器接口
  • 重写抽象方法
  • web.xml配置(注解)
@WebListener
public class MyServletContextListener implements ServletContextListener {
    @Override
    /**
     *  ServletContext对象,被创建,调用
     */
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("ServletContext域对象创建");
    }

    @Override
    /**
     *   ServletContext对象,被销毁前调用
     */
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("ServletContext域对象销毁");
    }
}


/*
<listener>
        <listener-class>com.listener.MyServletContextListener</listener-class>
</listener>
*/
  • 24
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我超爱写bug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值