基于Listener監(jiān)聽器生命周期的示例分析

小編給大家分享一下基于Listener監(jiān)聽器生命周期的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

站在用戶的角度思考問題,與客戶深入溝通,找到邳州網(wǎng)站設(shè)計與邳州網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設(shè)計制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋邳州地區(qū)。

一、Listener生命周期

listener是web三大組件之一,是servlet監(jiān)聽器,用來監(jiān)聽請求,監(jiān)聽服務(wù)端的操作。

listener分為:(都是接口類,必須實現(xiàn)相應(yīng)方法)

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)用(請求結(jié)束)

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) 當(dāng)該類實例設(shè)置進session域中時調(diào)用
valueUnbound(HttpSessionBindingEvent event) 當(dāng)該類的實例從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)聽器類似,不在重復(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>

運行結(jié)果:

基于Listener監(jiān)聽器生命周期的示例分析

基于Listener監(jiān)聽器生命周期的示例分析

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)聽器設(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é)果如下:

基于Listener監(jiān)聽器生命周期的示例分析

注意:其中遇到一個問題:就是在啟動tomcat的時候servletcontextListener監(jiān)聽執(zhí)行了兩次,最后刪除掉server.xml中 Context 的手動配置,這樣就不會加載兩次了。

看完了這篇文章,相信你對“基于Listener監(jiān)聽器生命周期的示例分析”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

名稱欄目:基于Listener監(jiān)聽器生命周期的示例分析
文章URL:http://muchs.cn/article44/johiee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名網(wǎng)站內(nèi)鏈、品牌網(wǎng)站設(shè)計、動態(tài)網(wǎng)站、、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)