Listener
觀察者設計模式:
它是事件驅動的一種體現形式。就好比在做什么事情的時候被人盯著。當對應做到某件事時,觸發事件。
觀察者模式通常由以下三部分組成:
?1. 事件源:觸發事件的對象。
2.? 事件:觸發的動作,里面封裝了事件源。
3. 監聽器:當事件源觸發事件時,要做的事情。一般是一個接口,由使用者來實現。
Listener:
- Listener是監聽器,可以對,對象的創建、銷毀、域對象屬性的變化、會話進行監聽
- 監聽器都是基于觀察者設計模式的,Servlet一共有八個監聽器都是接口形式的。
監聽對象的監聽器:
ServletContextListener:
ServletContextListener:用于監聽ServletContext對象的創建和銷毀
| void | contextInitialized(ServletContextEvent sce) | 對象創建時執行該方法 |
| void | contextDestroyed(ServletContextEvent sce) | 對象銷毀時執行該方法 |
ServletContextEvent:
代表事件對象,事件對象封裝了事件源,也就是ServletContext,真正的事件指的是創建或銷毀ServletContext對象的操作
HttpSessionListener:
用于監聽HttpSession對象的創建和銷毀
| void | sessionCreated(HttpSessionEvent se) | 對象創建時執行該方法 |
| void | sessionDestroyed(HttpSessionEvent se) 對象銷毀時執行該方法 |
HttpSessionEvent:
代表事件對象,事件對象封裝了事件源,也就是HttpSession,真正的事件指的是創建或銷毀HttpSession對象的操作
ServletRequestListener:
用于監聽ServletRequest對象的創建和銷毀
| void | requestInitialized (ServletRequestEvent sre) | 對象創建時執行該方法 |
| void | requestDestroyed(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應用域中屬性的變化
| void | attributeAdded(ServletContextAttributeEvent scae) | 域中添加屬性時執行該方法 |
| void | attributeRemoved(ServletContextAttributeEvent scae) | 域中移除屬性時執行該方法 |
| void | attributeReplaced(ServletContextAttributeEvent scae) | 域中替換屬性時執行該方法 |
ServletContextAttributeEvent:
代表事件對象,事件對象封裝了事件源,也就是ServletContext,真正的事件指的是添加、移除、替換應用域中屬性的操作
HttpSessionAttributeListener:
用于監聽HttpSession會話域中屬性的變化
| void | attributeAdded(HttpSessionBindingEvent se) | 域中添加屬性時執行該方法 |
| void | attributeRemoved(HttpSessionBindingEvent se) | 域中移除屬性時執行該方法 |
| void | attributeReplaced(HttpSessionBindingEvent se) | 域中替換屬性時執行該方法 |
HttpSessionBindingEvent:
代表事件對象,事件對象封裝了事件源,也就是HttpSession,真正的事件指的是添加、移除、替換應用域中屬性的操作
ServletRequestAttributeListener:
用于監聽ServletRequest請求域中屬性的變化
| void | attributeAdded(ServletRequestAttributeEvent srae) | 域中添加屬性時執行該方法 |
| void | attributeRemoved(ServletRequestAttributeEvent srae) | 域中移除屬性時執行該方法 |
| void | attributeReplaced(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:
用于感知對象和會話域綁定的監聽器
| void | valueBound(HttpSessionBindingEvent event) | 數據添加到會話域中時執行該方法 |
| void | valueUnbound(HttpSessionBindingEvent event) | 數據從會話域中移除時執行該方法 |
HttpSessionBindingEvent:
代表事件對象,事件對象封裝了事件源,也就是HttpSession,真正的事件指的是添加、移除會話域中數據的操作
HttpSessionActivationListener:
用于感知會話域中對象鈍化和活化的監聽器
| void | sessionWillPassivate(HttpSessionEvent se) | 會話域中數據鈍化時執行該方法 |
| void | sessionDidActivate(HttpSessionEvent se) | 會話域中數據活化時執行該方法 |
HttpSessionEvent:
代表事件對象,事件對象封裝了事件源,也就是HttpSession,真正的事件指的是鈍化、活化的操作
總結
- 上一篇: c语言 二维数组 文库,c语言二维数组练
- 下一篇: vue css load,vue css