基于Listener監(jiān)聽器生命周期(詳解)
一、Listener生命周期
listener是web三大組件之一,是servlet監(jiān)聽器,用來(lái)監(jiān)聽請(qǐng)求,監(jiān)聽服務(wù)端的操作。
listener分為:(都是接口類,必須實(shí)現(xiàn)相應(yīng)方法)
1.生命周期監(jiān)聽器(3個(gè))
ServletContextListener requestDestroyed 在容器啟動(dòng)時(shí)被調(diào)用(在servlet被實(shí)例化前執(zhí)行) requestInitialized 在容器銷毀時(shí)調(diào)用(在servlet被銷毀后執(zhí)行) HttpSessionListener sessionCreated 在HttpSession創(chuàng)建后調(diào)用 sessionDestroyed 在HttpSession銷毀前調(diào)用(執(zhí)行session.invalidate();方法) ServletRequestListener requestDestroyed 在request對(duì)象創(chuàng)建后調(diào)用(發(fā)起請(qǐng)求) requestInitialized 在request對(duì)象銷毀前調(diào)用(請(qǐng)求結(jié)束)
2.屬性變化監(jiān)聽器(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)聽器接口除了傳參不同,方法名都是一樣的。分別監(jiān)聽application,session,request對(duì)象的屬性變化。
3.session中指定類屬性變化監(jiān)聽器(2)
HttpSessionBindingListener valueBound(HttpSessionBindingEvent event) 當(dāng)該類實(shí)例設(shè)置進(jìn)session域中時(shí)調(diào)用 valueUnbound(HttpSessionBindingEvent event) 當(dāng)該類的實(shí)例從session域中移除時(shí)調(diào)用 HttpSessionActivationListener sessionWillPassivate(HttpSessionEvent se) sessionDidActivate(HttpSessionEvent se)
二、測(cè)試范例
1.生命周期監(jiān)聽:
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銷毀");
}
}
其他兩個(gè)監(jiān)聽器類似,不在重復(fù)貼出。
在web.xml中配置
<!-- 監(jiān)聽器 --> <!-- servlet監(jiān)聽器 --> <listener> <listener-class>study.myListener.ServletContentAttribute_Listener</listener-class> </listener> <!-- session監(jiān)聽器 --> <listener> <listener-class>study.myListener.HttpSessionAttribute_Listener</listener-class> </listener> <!-- request監(jiān)聽器--> <listener> <listener-class>study.myListener.ServletRequestAttribute_Listener</listener-class> </listener>
運(yùn)行結(jié)果:


2.屬性監(jiān)聽:
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)聽器類似,不在贅訴。接下來(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)聽器設(shè)置</title>
</head>
<body>
<%
/**
* servlet監(jiān)聽
*/
application.setAttribute("name","changxiang");
application.setAttribute("name","小Cai先森");
application.removeAttribute("name");
/**
* session監(jiān)聽
*/
session.setAttribute("sessionName","changxiang");
session.setAttribute("sessionName","小Cai先森");
session.removeAttribute("sessionName");
session.invalidate();
/**
* request監(jiān)聽
*/
request.setAttribute("requestName","changxiang");
request.setAttribute("requestName","小Cai先森");
request.removeAttribute("requestName");
%>
</body>
</html>
執(zhí)行結(jié)果如下:

注意:其中遇到一個(gè)問題:就是在啟動(dòng)tomcat的時(shí)候servletcontextListener監(jiān)聽執(zhí)行了兩次,最后刪除掉server.xml中 Context 的手動(dòng)配置,這樣就不會(huì)加載兩次了。
以上這篇基于Listener監(jiān)聽器生命周期(詳解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Scala偏函數(shù)與偏應(yīng)用函數(shù)超詳細(xì)講解
Scala是一種多范式的編程語(yǔ)言,支持面向?qū)ο蠛秃瘮?shù)式編程。Scala也支持異常處理,即在程序運(yùn)行過程中發(fā)生意外或錯(cuò)誤時(shí),采取相應(yīng)的措施2023-04-04
Java實(shí)現(xiàn)順序表和鏈表結(jié)構(gòu)
大家好,本篇文章主要講的是Java實(shí)現(xiàn)順序表和鏈表結(jié)構(gòu),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
Mybatis以main方法形式調(diào)用dao層執(zhí)行代碼實(shí)例
這篇文章主要介紹了Mybatis以main方法形式調(diào)用dao層執(zhí)行代碼實(shí)例,MyBatis 是一款優(yōu)秀的持久層框架,MyBatis 免除了幾乎所有的 JDBC 代碼以及設(shè)置參數(shù)和獲取結(jié)果集的工作,需要的朋友可以參考下2023-08-08
本地MinIO存儲(chǔ)服務(wù)Java遠(yuǎn)程調(diào)用上傳文件的操作過程
MinIO是一款高性能、分布式的對(duì)象存儲(chǔ)系統(tǒng),它可以100%的運(yùn)行在標(biāo)準(zhǔn)硬件上,即X86等低成本機(jī)器也能夠很好的運(yùn)行MinIO,這篇文章主要介紹了本地MinIO存儲(chǔ)服務(wù)Java遠(yuǎn)程調(diào)用上傳文件的操作過程,需要的朋友可以參考下2023-11-11
tk.mybatis如何擴(kuò)展自己的通用mapper
這篇文章主要介紹了tk.mybatis如何擴(kuò)展自己的通用mapper操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot 將多個(gè)Excel打包下載的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot 將多個(gè)Excel打包下載的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
Spring 中的 ResourceLoader實(shí)例詳解
Spring框架提供了ResourceLoader接口,用于加載資源文件,DefaultResourceLoader是其基本實(shí)現(xiàn),只能加載單個(gè)資源,而ResourcePatternResolver繼承自ResourceLoader,增加了按模式加載多個(gè)資源的能力,感興趣的朋友一起看看吧2024-11-11

