欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Listener

发布时间:2025/3/15 编程问答 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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 {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("监听到对象的创建");// 获取对象System.out.println(sce.getServletContext());}@Overridepublic 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 {@Overridepublic 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");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {System.out.println("监听到对象销毁");} }

监听器

@WebListener public class ServletContextAttributeListenerDemo01 implements ServletContextAttributeListener {@Overridepublic void attributeAdded(ServletContextAttributeEvent event) {System.out.println("监听到了属性的添加");// 获取应用域对象ServletContext servletContext = event.getServletContext();// 获取属性Object username = servletContext.getAttribute("username");System.out.println(username);}@Overridepublic void attributeReplaced(ServletContextAttributeEvent event) {System.out.println("监听到了属性的替换");// 获取应用域对象ServletContext servletContext = event.getServletContext();// 获取属性Object username = servletContext.getAttribute("username");System.out.println(username);}@Overridepublic 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,真正的事件指的是钝化、活化的操作

总结

以上是生活随笔为你收集整理的Listener的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。