Listener

观察者设计模式:

它是事件驱动的一种体现形式。就好比在做什么事情的时候被人盯着。当对应做到某件事时,触发事件。

观察者模式通常由以下三部分组成:

​1. 事件源:触发事件的对象。
2.​ 事件:触发的动作,里面封装了事件源。
3. 监听器:当事件源触发事件时,要做的事情。一般是一个接口,由使用者来实现。

Listener:
  • Listener是监听器,可以对,对象的创建、销毁、域对象属性的变化、会话进行监听
  • 监听器都是基于观察者设计模式的,Servlet一共有八个监听器都是接口形式的。
监听对象的监听器:
ServletContextListener:

ServletContextListener:用于监听ServletContext对象的创建和销毁

返回值方法名说明
voidcontextInitialized(ServletContextEvent sce)对象创建时执行该方法
voidcontextDestroyed(ServletContextEvent sce)对象销毁时执行该方法

ServletContextEvent:

代表事件对象,事件对象封装了事件源,也就是ServletContext,真正的事件指的是创建或销毁ServletContext对象的操作

HttpSessionListener:

用于监听HttpSession对象的创建和销毁

返回值方法名说明
voidsessionCreated(HttpSessionEvent se)对象创建时执行该方法
voidsessionDestroyed(HttpSessionEvent se) 对象销毁时执行该方法

HttpSessionEvent:

代表事件对象,事件对象封装了事件源,也就是HttpSession,真正的事件指的是创建或销毁HttpSession对象的操作

ServletRequestListener:

用于监听ServletRequest对象的创建和销毁

返回值方法名说明
voidrequestInitialized (ServletRequestEvent sre)对象创建时执行该方法
voidrequestDestroyed(ServletRequestEvent sre)对象销毁时执行该方法

ServletRequestEvent:

代表事件对象,事件对象封装了事件源,也就是ServletRequest,真正的事件指的是创建或销毁ServletRequest对象的操作

演示:
@WebListener
public class ListenerDemo01 implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("监听到对象的创建");

        // 获取对象
        System.out.println(sce.getServletContext());
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("监听到对象销毁");
    }
}
监听域对象属性变化的监听器:
ServletContextAttributeListener:

用于监听ServletContext应用域中属性的变化

返回值方法名说明
voidattributeAdded(ServletContextAttributeEvent scae)域中添加属性时执行该方法
voidattributeRemoved(ServletContextAttributeEvent scae)域中移除属性时执行该方法
voidattributeReplaced(ServletContextAttributeEvent scae)域中替换属性时执行该方法

ServletContextAttributeEvent:

代表事件对象,事件对象封装了事件源,也就是ServletContext,真正的事件指的是添加、移除、替换应用域中属性的操作

HttpSessionAttributeListener:

用于监听HttpSession会话域中属性的变化

返回值方法名说明
voidattributeAdded(HttpSessionBindingEvent se)域中添加属性时执行该方法
voidattributeRemoved(HttpSessionBindingEvent se)域中移除属性时执行该方法
voidattributeReplaced(HttpSessionBindingEvent se)域中替换属性时执行该方法

HttpSessionBindingEvent:

代表事件对象,事件对象封装了事件源,也就是HttpSession,真正的事件指的是添加、移除、替换应用域中属性的操作

ServletRequestAttributeListener:

用于监听ServletRequest请求域中属性的变化

返回值方法名说明
voidattributeAdded(ServletRequestAttributeEvent srae)域中添加属性时执行该方法
voidattributeRemoved(ServletRequestAttributeEvent srae)域中移除属性时执行该方法
voidattributeReplaced(ServletRequestAttributeEvent srae)域中替换属性时执行该方法

ServletRequestAttributeEvent:

代表事件对象,事件对象封装了事件源,也就是ServletRequestAttribute,真正的事件指的是添加、移除、替换应用域中属性的操作

演示:

执行添加、替换、移除的类

@WebListener
public class ServletContextListenerDemo01 implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("监听到对象的创建");

        // 获取对象
        ServletContext servletContext = sce.getServletContext();
        System.out.println(servletContext);

        // 添加属性
        servletContext.setAttribute("username", "itzhuzhu");

        // 替换属性
        servletContext.setAttribute("username", "hanxin");

        // 移除属性
        servletContext.removeAttribute("username");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("监听到对象销毁");
    }
}

监听器

@WebListener
public class ServletContextAttributeListenerDemo01 implements ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
        System.out.println("监听到了属性的添加");

        // 获取应用域对象
        ServletContext servletContext = event.getServletContext();
        // 获取属性
        Object username = servletContext.getAttribute("username");
        System.out.println(username);
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent event) {
        System.out.println("监听到了属性的替换");

        // 获取应用域对象
        ServletContext servletContext = event.getServletContext();
        // 获取属性
        Object username = servletContext.getAttribute("username");
        System.out.println(username);
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent event) {
        System.out.println("监听到了属性的移除");

        // 获取应用域对象
        ServletContext servletContext = event.getServletContext();
        // 获取属性
        Object username = servletContext.getAttribute("username");
        System.out.println(username);
    }
}
配置文件形式配置监听器:
    <listener>
        <listener-class>com.listener.ServletContextAttributeListenerDemo01</listener-class>
    </listener>

    <listener>
        <listener-class>com.listener.ServletContextListenerDemo01</listener-class>
    </listener>
监听会话相关的感知型监听器:

感知型监听器:当监听器配置好了以后还需要用注解xml做一些配置,但是感知性监听器是不需要的,定义好了以后就可以直接使用了

HttpSessionBinderListener:

用于感知对象和会话域绑定的监听器

返回值方法名说明
voidvalueBound(HttpSessionBindingEvent event)数据添加到会话域中时执行该方法
voidvalueUnbound(HttpSessionBindingEvent event)数据从会话域中移除时执行该方法

HttpSessionBindingEvent:

代表事件对象,事件对象封装了事件源,也就是HttpSession,真正的事件指的是添加、移除会话域中数据的操作

HttpSessionActivationListener:

用于感知会话域中对象钝化和活化的监听器

返回值方法名说明
voidsessionWillPassivate(HttpSessionEvent se)会话域中数据钝化时执行该方法
voidsessionDidActivate(HttpSessionEvent se)会话域中数据活化时执行该方法

HttpSessionEvent:

代表事件对象,事件对象封装了事件源,也就是HttpSession,真正的事件指的是钝化、活化的操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

itzhuzhu.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值