1. 程式人生 > >Listener監聽器生命周期

Listener監聽器生命周期

settings 創建 min con setattr r.java file 變化 name

一、Listener生命周期

  listener是web三大組件之一,是servlet監聽器,用來監聽請求,監聽服務端的操作。

  listener分為:(都是接口類,必須實現相應方法)

  1.生命周期監聽器(3個)
      • ServletContextListener
        • requestDestroyed 在容器啟動時被調用(在servlet被實例化前執行)
        • requestInitialized 在容器銷毀時調用(在servlet被銷毀後執行)
      • HttpSessionListener
        • sessionCreated 在HttpSession創建後調用
        • sessionDestroyed 在HttpSession銷毀前調用(執行session.invalidate();方法)
      • ServletRequestListener
        • requestDestroyed 在request對象創建後調用(發起請求)
        • requestInitialized 在request對象銷毀前調用(請求結束)
  2.屬性變化監聽器(3個)
        • attributeAdded(ServletContextAttributeEvent event)  向appliction中添加屬性時調用
        • attributeRemoved(ServletContextAttributeEvent event)  從appliction中刪除屬性時調用
        • attributeReplaced(ServletContextAttributeEvent event)  替換application中的屬性時調用
      • HttpSessionAttributeListener
        • attributeAdded(HttpSessionBindingEvent event)
        • attributeRemoved(HttpSessionBindingEvent event)
        • attributeReplaced(HttpSessionBindingEvent event)
      • ServletRequestAttributeListener
        • attributeAdded(ServletRequestAttributeEvent event)
        • attributeRemoved(ServletRequestAttributeEvent event)
        • attributeReplaced(ServletRequestAttributeEvent event)

       以上監聽器接口除了傳參不同,方法名都是一樣的。分別監聽application,session,request對象的屬性變化。

 3.session中指定類屬性變化監聽器(2)
      • HttpSessionBindingListener 
        • valueBound(HttpSessionBindingEvent event) 當該類實例設置進session域中時調用
        • valueUnbound(HttpSessionBindingEvent event) 當該類的實例從session域中移除時調用
      • HttpSessionActivationListener 
        • sessionWillPassivate(HttpSessionEvent se)
        • sessionDidActivate(HttpSessionEvent se)
            

二、測試範例

 1.生命周期監聽:

  ServletContentAttribute_Listener.java

 1 public class ServletContentAttribute_Listener implements ServletContextListener {
 2     /**
 3      * ServletContextListener實現方法
 4      * @param sce
 5      */
 6     public void contextInitialized(ServletContextEvent sce) {
 7         System.out.println("ServletContextListener初始化");
 8     }
 9 
10     public void contextDestroyed(ServletContextEvent sce) {
11         System.out.println("ServletContextListener銷毀");
12     }
13 }

  其他兩個監聽器類似,不在重復貼出。

  在web.xml中配置

 1 <!-- 監聽器 -->
 2   <!-- servlet監聽器 -->
 3   <listener>
 4     <listener-class>study.myListener.ServletContentAttribute_Listener</listener-class>
 5   </listener>
 6 
 7   <!-- session監聽器 -->
 8   <listener>
 9     <listener-class>study.myListener.HttpSessionAttribute_Listener</listener-class>
10   </listener>
11   
12   <!-- request監聽器-->
13   <listener>
14     <listener-class>study.myListener.ServletRequestAttribute_Listener</listener-class>
15   </listener>

  運行結果:

 技術分享

  技術分享

 2.屬性監聽:

  ServletContentAttribute_Listener.java

 1 public class ServletContentAttribute_Listener implements ServletContextAttributeListener{
 2 
 3     /**
 4      * ServletContextAttributeListener實現方法
 5      * @param event
 6      */
 7     public void attributeAdded(ServletContextAttributeEvent event) {
 8         String meg = MessageFormat.format("ServletContent添加屬性:{0},屬性值:{1}",event.getName(),event.getValue());
 9         System.out.println(meg);
10     }
11 
12     public void attributeRemoved(ServletContextAttributeEvent event) {
13         String meg = MessageFormat.format("ServletContent刪除屬性:{0},屬性值:{1}",event.getName(),event.getValue());
14         System.out.println(meg);
15     }
16 
17     public void attributeReplaced(ServletContextAttributeEvent event) {
18         String meg = MessageFormat.format("ServletContent替換屬性:{0},屬性值:{1}",event.getName(),event.getValue());
19         System.out.println(meg);
20     }
21 
22 }

  另外兩個監聽器類似,不在贅訴。接下來用jsp頁面測試

  listenerDemo.jsp

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: Administrator
 4   Date: 2017/10/17
 5   Time: 15:28
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <title>監聽器設置</title>
12 </head>
13 <body>
14     <%
15         /**
16          * servlet監聽
17          */
18         application.setAttribute("name","changxiang");
19         application.setAttribute("name","小Cai先森");
20         application.removeAttribute("name");
21 
22         /**
23          * session監聽
24          */
25         session.setAttribute("sessionName","changxiang");
26         session.setAttribute("sessionName","小Cai先森");
27         session.removeAttribute("sessionName");
28         session.invalidate();
29         /**
30          * request監聽
31          */
32         request.setAttribute("requestName","changxiang");
33         request.setAttribute("requestName","小Cai先森");
34         request.removeAttribute("requestName");
35     %>
36 </body>
37 </html>

 執行結果如下:

 技術分享

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

解決思路:http://blog.csdn.net/shaokai132333/article/details/53328258

  

Listener監聽器生命周期