基于Listener監(jiān)聽(tīng)器生命周期(詳解)
一、Listener生命周期
listener是web三大組件之一,是servlet監(jiān)聽(tīng)器,用來(lái)監(jiān)聽(tīng)請(qǐng)求,監(jiān)聽(tīng)服務(wù)端的操作。
listener分為:(都是接口類(lèi),必須實(shí)現(xiàn)相應(yīng)方法)
1.生命周期監(jiān)聽(tīng)器(3個(gè))
ServletContextListener requestDestroyed 在容器啟動(dòng)時(shí)被調(diào)用(在servlet被實(shí)例化前執(zhí)行) requestInitialized 在容器銷(xiāo)毀時(shí)調(diào)用(在servlet被銷(xiāo)毀后執(zhí)行) HttpSessionListener sessionCreated 在HttpSession創(chuàng)建后調(diào)用 sessionDestroyed 在HttpSession銷(xiāo)毀前調(diào)用(執(zhí)行session.invalidate();方法) ServletRequestListener requestDestroyed 在request對(duì)象創(chuàng)建后調(diào)用(發(fā)起請(qǐng)求) requestInitialized 在request對(duì)象銷(xiāo)毀前調(diào)用(請(qǐng)求結(jié)束)
2.屬性變化監(jiān)聽(tīng)器(3個(gè))
attributeAdded(ServletContextAttributeEvent event)向appliction中添加屬性時(shí)調(diào)用 attributeRemoved(ServletContextAttributeEvent event)從appliction中刪除屬性時(shí)調(diào)用 attributeReplaced(ServletContextAttributeEvent event)替換application中的屬性時(shí)調(diào)用 HttpSessionAttributeListener attributeAdded(HttpSessionBindingEvent event) attributeRemoved(HttpSessionBindingEvent event) attributeReplaced(HttpSessionBindingEvent event) ServletRequestAttributeListener attributeAdded(ServletRequestAttributeEvent event) attributeRemoved(ServletRequestAttributeEvent event) attributeReplaced(ServletRequestAttributeEvent event)
以上監(jiān)聽(tīng)器接口除了傳參不同,方法名都是一樣的。分別監(jiān)聽(tīng)application,session,request對(duì)象的屬性變化。
3.session中指定類(lèi)屬性變化監(jiān)聽(tīng)器(2)
HttpSessionBindingListener valueBound(HttpSessionBindingEvent event) 當(dāng)該類(lèi)實(shí)例設(shè)置進(jìn)session域中時(shí)調(diào)用 valueUnbound(HttpSessionBindingEvent event) 當(dāng)該類(lèi)的實(shí)例從session域中移除時(shí)調(diào)用 HttpSessionActivationListener sessionWillPassivate(HttpSessionEvent se) sessionDidActivate(HttpSessionEvent se)
二、測(cè)試范例
1.生命周期監(jiān)聽(tīng):
ServletContentAttribute_Listener.java
public class ServletContentAttribute_Listener implements ServletContextListener { /** * ServletContextListener實(shí)現(xiàn)方法 * @param sce */ public void contextInitialized(ServletContextEvent sce) { System.out.println("ServletContextListener初始化"); } public void contextDestroyed(ServletContextEvent sce) { System.out.println("ServletContextListener銷(xiāo)毀"); } }
其他兩個(gè)監(jiān)聽(tīng)器類(lèi)似,不在重復(fù)貼出。
在web.xml中配置
<!-- 監(jiān)聽(tīng)器 --> <!-- servlet監(jiān)聽(tīng)器 --> <listener> <listener-class>study.myListener.ServletContentAttribute_Listener</listener-class> </listener> <!-- session監(jiān)聽(tīng)器 --> <listener> <listener-class>study.myListener.HttpSessionAttribute_Listener</listener-class> </listener> <!-- request監(jiān)聽(tīng)器--> <listener> <listener-class>study.myListener.ServletRequestAttribute_Listener</listener-class> </listener>
運(yùn)行結(jié)果:
2.屬性監(jiān)聽(tīng):
ServletContentAttribute_Listener.java
public class ServletContentAttribute_Listener implements ServletContextAttributeListener{ /** * ServletContextAttributeListener實(shí)現(xiàn)方法 * @param event */ public void attributeAdded(ServletContextAttributeEvent event) { String meg = MessageFormat.format("ServletContent添加屬性:{0},屬性值:{1}",event.getName(),event.getValue()); System.out.println(meg); } public void attributeRemoved(ServletContextAttributeEvent event) { String meg = MessageFormat.format("ServletContent刪除屬性:{0},屬性值:{1}",event.getName(),event.getValue()); System.out.println(meg); } public void attributeReplaced(ServletContextAttributeEvent event) { String meg = MessageFormat.format("ServletContent替換屬性:{0},屬性值:{1}",event.getName(),event.getValue()); System.out.println(meg); } }
另外兩個(gè)監(jiān)聽(tīng)器類(lèi)似,不在贅訴。接下來(lái)用jsp頁(yè)面測(cè)試
listenerDemo.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2017/10/17 Time: 15:28 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>監(jiān)聽(tīng)器設(shè)置</title> </head> <body> <% /** * servlet監(jiān)聽(tīng) */ application.setAttribute("name","changxiang"); application.setAttribute("name","小Cai先森"); application.removeAttribute("name"); /** * session監(jiān)聽(tīng) */ session.setAttribute("sessionName","changxiang"); session.setAttribute("sessionName","小Cai先森"); session.removeAttribute("sessionName"); session.invalidate(); /** * request監(jiān)聽(tīng) */ request.setAttribute("requestName","changxiang"); request.setAttribute("requestName","小Cai先森"); request.removeAttribute("requestName"); %> </body> </html>
執(zhí)行結(jié)果如下:
注意:其中遇到一個(gè)問(wèn)題:就是在啟動(dòng)tomcat的時(shí)候servletcontextListener監(jiān)聽(tīng)執(zhí)行了兩次,最后刪除掉server.xml中 Context 的手動(dòng)配置,這樣就不會(huì)加載兩次了。
以上這篇基于Listener監(jiān)聽(tīng)器生命周期(詳解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Security基本架構(gòu)與初始化操作流程詳解
這篇文章主要介紹了Spring Security基本架構(gòu)與初始化操作流程,Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪(fǎng)問(wèn)控制解決方案的安全框架2023-03-03springboot關(guān)于容器啟動(dòng)事件總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于springboot容器啟動(dòng)事件相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。2019-10-10Spring Boot 功能整合的實(shí)現(xiàn)
Spring Boot生態(tài)豐富,集成也不算困難。本文簡(jiǎn)單的介紹下功能整合的步驟,最后提供一個(gè)具體的實(shí)現(xiàn)例子,學(xué)習(xí)Spring Boot的同學(xué)可以參考下2021-05-05基于Mybatis-Plus的CRUD的實(shí)現(xiàn)
這篇文章主要介紹了基于Mybatis-Plus的CRUD的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11SpringBoot3結(jié)合gRpc實(shí)現(xiàn)遠(yuǎn)程服務(wù)調(diào)用的流程步驟
gRPC是一個(gè)現(xiàn)代開(kāi)源高性能遠(yuǎn)程過(guò)程調(diào)用(RPC)框架,可以在任何環(huán)境中運(yùn)行,它由Google開(kāi)發(fā),旨在幫助開(kāi)發(fā)人員更輕松地構(gòu)建分布式應(yīng)用,特別是當(dāng)代碼可能在不同地方運(yùn)行的時(shí)候,本文介紹了SpringBoot3結(jié)合gRpc實(shí)現(xiàn)遠(yuǎn)程服務(wù)調(diào)用的流程步驟,需要的朋友可以參考下2024-07-07詳細(xì)總結(jié)各種排序算法(Java實(shí)現(xiàn))
下面小編就為大家?guī)?lái)一篇詳細(xì)總結(jié)各種排序算法(Java實(shí)現(xiàn))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09HttpServletRequestWrapper干預(yù)Request處理流程解析
這篇文章主要分析在?Tomcat的處理?http?請(qǐng)求的流程中干預(yù)?Request對(duì)象,?通過(guò)基于HttpServletRequestWrapper和?Filter組合進(jìn)行干預(yù),有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-09-09