【JavaWeb】监听器 Listener


一、监听器是什么

  • 类似 JS 的事件

    • 被观察的对象发生某些情况时,自动触发代码的执行
  • 对域对象要发生的方法进行监听

  • 并不监听web项目中的所有组件,仅仅是对三大域对象做相关的事件监听

  • 监听器是GOF设计模式中,观察者模式的典型案例

    • 观察者模式: 当被观察的对象发生某些改变时, 观察者自动采取对应的行动的一种设计模式

二、监听器的分类

web中定义八个监听器接口作为监听器的规范,
八个接口按照不同的标准可以形成不同的分类 :

  • 按监听的对象划分

    • application域监听器
      • ServletContextListener
      • ServletContextAttributeListener
    • session域监听器
      • HttpSessionListener
      • HttpSessionAttributeListener
      • HttpSessionBindingListener
      • HttpSessionActivationListener
    • request域监听器
      • ServletRequestListener
      • ServletRequestAttributeListener
  • 按监听的事件分

    • 域对象的创建和销毁监听器
      • ServletContextListener
      • HttpSessionListener
      • ServletRequestListener
    • 域对象数据增删改事件监听器
      • ServletContextAttributeListener
      • HttpSessionAttributeListener
      • ServletRequestAttributeListener
    • 其他监听器
      • HttpSessionBindingListener
      • HttpSessionActivationListener

三、监听器的六个主要接口

3.1 application域监听器

ServletContextListener 监听ServletContext对象的创建与销毁

方法名 作用
contextInitialized(ServletContextEvent sce) ServletContext创建时调用
contextDestroyed(ServletContextEvent sce) ServletContext销毁时调用
  • ServletContextEvent对象代表从ServletContext对象身上捕获到的事件,
  • 通过这个事件对象我们可以获取到ServletContext对象。

ServletContextAttributeListener 监听ServletContext中属性的添加、移除和修改

方法名 作用
attributeAdded(ServletContextAttributeEvent scab) 向ServletContext中添加属性时调用
attributeRemoved(ServletContextAttributeEvent scab) 从ServletContext中移除属性时调用
attributeReplaced(ServletContextAttributeEvent scab) 当ServletContext中的属性被修改时调用
  • ServletContextAttributeEvent对象代表属性变化事件,它包含的方法如下:
方法名 作用
getName() 获取修改或添加的属性名
getValue() 获取被修改或添加的属性值
getServletContext() 获取ServletContext对象

测试代码 :

3.1.1 定义监听器

@WebListener
public class ApplicationListener implements ServletContextListener , ServletContextAttributeListener {
   
   
    // 监听初始化
    @Override
    public void contextInitialized(ServletContextEvent sce) {
   
   
        ServletContext application = sce.getServletContext();
        System.out.println("application"+application.hashCode()+" initialized");
    }
    // 监听销毁
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
   
   
        ServletContext application = sce.getServletContext();
        System.out.println("application"+application.hashCode()+" destroyed");
    }

    // 监听数据增加
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
   
   
        String name = scae.getName();
        Object value = scae.getValue();
        ServletContext application = scae.getServletContext();
        System.out.println("application"+application.hashCode()+" add:"+name+"="+value);
    }

    // 监听数据移除
    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
   
   
        String name = scae.getName();
        Object value = scae.getValue();
        ServletContext application = scae.getServletContext();
        System.out.println("application"+application.hashCode()+" remove:"+name+"="+value);
    }
    // 监听数据修改
    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
   
   
        String name = scae.getName();
        Object value = scae.getValue();
        ServletContext application = scae.getServletContext();
        Object newValue = application.getAttribute(name);
        System.out.println("application"+application.hashCode()+" change:"+name+"="+value+" to "+newValue);
    }


}

3.1.2 定义触发监听器的代码

// ServletA用于向application域中放入数据
@WebServlet(urlPatterns = "/servletA",name = "servletAName")
public class ServletA extends HttpServlet {
   
   
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
   
        // 向application域中放入数据
        ServletContext application = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

「已注销」

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

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

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

打赏作者

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

抵扣说明:

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

余额充值