海安网站优化,宜宾建设网官网,全屋定制十大名牌排名,轻量的wordpress观察者设计模式#xff1a; 它是事件驱动的一种体现形式。就好比在做什么事情的时候被人盯着。当对应做到某件事时#xff0c;触发事件。 观察者模式通常由以下三部分组成#xff1a; 1. 事件源#xff1a;触发事件的对象。 2. 事件#xff1a;触发的动作#xff0c;…观察者设计模式 它是事件驱动的一种体现形式。就好比在做什么事情的时候被人盯着。当对应做到某件事时触发事件。 观察者模式通常由以下三部分组成 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);}
}配置文件形式配置监听器 listenerlistener-classcom.listener.ServletContextAttributeListenerDemo01/listener-class/listenerlistenerlistener-classcom.listener.ServletContextListenerDemo01/listener-class/listener监听会话相关的感知型监听器 感知型监听器当监听器配置好了以后还需要用注解、xml做一些配置但是感知性监听器是不需要的定义好了以后就可以直接使用了 HttpSessionBinderListener 用于感知对象和会话域绑定的监听器 返回值方法名说明voidvalueBound(HttpSessionBindingEvent event)数据添加到会话域中时执行该方法voidvalueUnbound(HttpSessionBindingEvent event)数据从会话域中移除时执行该方法
HttpSessionBindingEvent 代表事件对象事件对象封装了事件源也就是HttpSession真正的事件指的是添加、移除会话域中数据的操作 HttpSessionActivationListener 用于感知会话域中对象钝化和活化的监听器 返回值方法名说明voidsessionWillPassivate(HttpSessionEvent se)会话域中数据钝化时执行该方法voidsessionDidActivate(HttpSessionEvent se)会话域中数据活化时执行该方法
HttpSessionEvent 代表事件对象事件对象封装了事件源也就是HttpSession真正的事件指的是钝化、活化的操作