基于Listener監(jiān)聽器生命周期(詳解)
一、Listener生命周期
listener是web三大組件之一,是servlet監(jiān)聽器,用來監(jiān)聽請求,監(jiān)聽服務端的操作。
listener分為:(都是接口類,必須實現(xiàn)相應方法)
1.生命周期監(jiān)聽器(3個)
ServletContextListener requestDestroyed 在容器啟動時被調(diào)用(在servlet被實例化前執(zhí)行) requestInitialized 在容器銷毀時調(diào)用(在servlet被銷毀后執(zhí)行) HttpSessionListener sessionCreated 在HttpSession創(chuàng)建后調(diào)用 sessionDestroyed 在HttpSession銷毀前調(diào)用(執(zhí)行session.invalidate();方法) ServletRequestListener requestDestroyed 在request對象創(chuàng)建后調(diào)用(發(fā)起請求) requestInitialized 在request對象銷毀前調(diào)用(請求結束)
2.屬性變化監(jiān)聽器(3個)
attributeAdded(ServletContextAttributeEvent event)向appliction中添加屬性時調(diào)用 attributeRemoved(ServletContextAttributeEvent event)從appliction中刪除屬性時調(diào)用 attributeReplaced(ServletContextAttributeEvent event)替換application中的屬性時調(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對象的屬性變化。
3.session中指定類屬性變化監(jiān)聽器(2)
HttpSessionBindingListener valueBound(HttpSessionBindingEvent event) 當該類實例設置進session域中時調(diào)用 valueUnbound(HttpSessionBindingEvent event) 當該類的實例從session域中移除時調(diào)用 HttpSessionActivationListener sessionWillPassivate(HttpSessionEvent se) sessionDidActivate(HttpSessionEvent se)
二、測試范例
1.生命周期監(jiān)聽:
ServletContentAttribute_Listener.java
public class ServletContentAttribute_Listener implements ServletContextListener {
/**
* ServletContextListener實現(xiàn)方法
* @param sce
*/
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContextListener初始化");
}
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContextListener銷毀");
}
}
其他兩個監(jiān)聽器類似,不在重復貼出。
在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>
運行結果:


2.屬性監(jiān)聽:
ServletContentAttribute_Listener.java
public class ServletContentAttribute_Listener implements ServletContextAttributeListener{
/**
* ServletContextAttributeListener實現(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);
}
}
另外兩個監(jiān)聽器類似,不在贅訴。接下來用jsp頁面測試
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)聽器設置</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í)行結果如下:

注意:其中遇到一個問題:就是在啟動tomcat的時候servletcontextListener監(jiān)聽執(zhí)行了兩次,最后刪除掉server.xml中 Context 的手動配置,這樣就不會加載兩次了。
以上這篇基于Listener監(jiān)聽器生命周期(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot3結合gRpc實現(xiàn)遠程服務調(diào)用的流程步驟
gRPC是一個現(xiàn)代開源高性能遠程過程調(diào)用(RPC)框架,可以在任何環(huán)境中運行,它由Google開發(fā),旨在幫助開發(fā)人員更輕松地構建分布式應用,特別是當代碼可能在不同地方運行的時候,本文介紹了SpringBoot3結合gRpc實現(xiàn)遠程服務調(diào)用的流程步驟,需要的朋友可以參考下2024-07-07
HttpServletRequestWrapper干預Request處理流程解析
這篇文章主要分析在?Tomcat的處理?http?請求的流程中干預?Request對象,?通過基于HttpServletRequestWrapper和?Filter組合進行干預,有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-09-09

