利用Listener實現網站累積訪問人數、最大同時在線人數、當前登錄用戶數的記錄


 1.網站全局統計變量類,只定義全局變量

 1 package com.lt.listener;
 2 
 3 import java.util.Date;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 import javax.servlet.http.HttpSession;
 8 /**
 9  * 網站全局變量類
10  * @author LIUTIE
11  *
12  */
13 public abstract class ApplicationConstants {
14     
15     /**
16      * 用戶登錄session名稱
17      */
18     public static final String LOGIN_SESSION_NAME = "userInfo";
19 
20     /**
21      * 索引所有的session  
22      * 用於單一登錄
23      */
24     public static Map<String,HttpSession> SESSION_MAP = new HashMap<>();
25     
26     /**
27      * 當前在線用戶數
28      */
29     public static int CURRENT_LOGIN_COUNT = 0;
30     
31     /**
32      * 歷史訪客總數
33      */
34     public static int TOTAL_HISTORY_COUNT = 0;
35     
36     /**
37      * 最高同時在線人數
38      */
39     public static int MAX_ONLINE_COUNT = 0;
40     
41     /**
42      * 服務器啟動時間
43      */
44     public static Date SERVER_START_DATE = new Date();
45     
46     /**
47      * 最高在線人數時間
48      */
49     public static Date MAX_ONLINE_COUNT_DATE = new Date();
50     
51     
52     
53 }
View Code

2.實現servletContext監聽,用於記錄服務器信息

 1 package com.lt.listener;
 2 
 3 import java.util.Date;
 4 
 5 import javax.servlet.ServletContextEvent;
 6 import javax.servlet.ServletContextListener;
 7 
 8 /**
 9  * servletContext監聽
10  * 記錄服務器信息 啟動關閉時間等
11  * @author LIUTIE
12  *
13  */
14 public class MyContextListener implements ServletContextListener {
15 
16     /**
17      * 服務器啟動時被調用
18      */
19     @Override
20     public void contextDestroyed(ServletContextEvent arg0) {
21         //記錄啟動時間
22         ApplicationConstants.SERVER_START_DATE = new Date();
23     }
24 
25     /**
26      * 服務器關閉時被調用
27      */
28     @Override
29     public void contextInitialized(ServletContextEvent arg0) {
30         //保存數據到硬盤
31         // TODO Auto-generated method stub
32     }
33 
34 }
View Code

3.實現 HttpSessionListener, HttpSessionAttributeListener監聽,用於記錄登錄信息、訪問總人數、在線人數,實現單一登錄等

  1 package com.lt.listener;
  2 
  3 import java.util.Date;
  4 
  5 import javax.servlet.http.HttpSession;
  6 import javax.servlet.http.HttpSessionAttributeListener;
  7 import javax.servlet.http.HttpSessionBindingEvent;
  8 import javax.servlet.http.HttpSessionEvent;
  9 import javax.servlet.http.HttpSessionListener;
 10 
 11 /**
 12  * session監聽
 13  * 記錄登錄信息 訪問總人數 在線人數等
 14  * 實現單一登錄
 15  * @author LIUTIE
 16  *
 17  */
 18 public class MySessionListener implements HttpSessionListener, HttpSessionAttributeListener {
 19 
 20     /**
 21      * session創建時被調用
 22      */
 23     @Override
 24     public void sessionCreated(HttpSessionEvent sessionEvent) {
 25         // 獲取創建的session
 26         HttpSession session = sessionEvent.getSession();
 27         // 添加到map
 28         ApplicationConstants.SESSION_MAP.put(session.getId(), session);
 29         // 訪問總人數++
 30         ApplicationConstants.TOTAL_HISTORY_COUNT++;
 31         // 如果map總數大於最高同時在線人數則更新最高在線人數及時間
 32         if (ApplicationConstants.MAX_ONLINE_COUNT < ApplicationConstants.SESSION_MAP.size()) {
 33             ApplicationConstants.MAX_ONLINE_COUNT = ApplicationConstants.SESSION_MAP.size();
 34             ApplicationConstants.MAX_ONLINE_COUNT_DATE = new Date();
 35         }
 36 
 37     }
 38 
 39     /**
 40      * session銷毀時被調用
 41      */
 42     @Override
 43     public void sessionDestroyed(HttpSessionEvent sessionEvent) {
 44         // 獲取即將被銷毀的session
 45         HttpSession session = sessionEvent.getSession();
 46         // 在map中根據key移除
 47         ApplicationConstants.SESSION_MAP.remove(session.getId());
 48     }
 49 
 50     /**
 51      * 添加session屬性時被調用
 52      */
 53     @Override
 54     public void attributeAdded(HttpSessionBindingEvent event) {
 55         // 判斷是否添加的用戶登錄信息session
 56         if (event.getName().equals(ApplicationConstants.LOGIN_SESSION_NAME)) {
 57             // 當前登錄用戶數++
 58             ApplicationConstants.CURRENT_LOGIN_COUNT++;
 59             // 是否在其他機器登錄處理
 60             isLoginInOtherPlace(event);
 61         }
 62     }
 63 
 64     /**
 65      * 移除session屬性時被調用
 66      */
 67     @Override
 68     public void attributeRemoved(HttpSessionBindingEvent event) {
 69         // 判斷是否移除的用戶登錄信息session
 70         if (event.getName().equals(ApplicationConstants.LOGIN_SESSION_NAME)) {
 71             // 當前登錄用戶數--
 72             ApplicationConstants.CURRENT_LOGIN_COUNT--;
 73             // 是否在其他機器登錄處理
 74             isLoginInOtherPlace(event);
 75         }
 76     }
 77 
 78     /**
 79      * 修改session屬性時被調用
 80      */
 81     @Override
 82     public void attributeReplaced(HttpSessionBindingEvent event) {
 83 
 84         // 判斷是否修改的用戶登錄信息session
 85         if (event.getName().equals(ApplicationConstants.LOGIN_SESSION_NAME)) {
 86             // 是否在其他機器登錄處理
 87             isLoginInOtherPlace(event);
 88         }
 89     }
 90 
 91     /**
 92      * 是否在其他機器登錄處理
 93      * 
 94      * @param event
 95      */
 96     private void isLoginInOtherPlace(HttpSessionBindingEvent event) {
 97         // 獲取添加的session
 98         HttpSession session = event.getSession();
 99         // 遍歷查找此用戶是否登錄
100         for (HttpSession s : ApplicationConstants.SESSION_MAP.values()) {
101             // 如果已經在其他機器登錄則使其失效
102             if (event.getValue().equals(s.getAttribute(ApplicationConstants.LOGIN_SESSION_NAME))
103                     && session.getId() != s.getId()) {
104                 // 使session失效
105                 session.invalidate();
106                 break;
107             }
108         }
109     }
110 }
View Code

4.實現 request監聽,用於記錄客戶信息 ip、url等

 1 package com.lt.listener;
 2 
 3 import javax.servlet.ServletRequestEvent;
 4 import javax.servlet.ServletRequestListener;
 5 import javax.servlet.http.HttpServletRequest;
 6 
 7 /**
 8  * request監聽 用於記錄客戶信息 ip、url等
 9  * 
10  * @author LIUTIE
11  *
12  */
13 public class MyRequestListener implements ServletRequestListener {
14 
15     /**
16      * request銷毀時調用
17      */
18     @Override
19     public void requestDestroyed(ServletRequestEvent event) {
20         // TODO Auto-generated method stub
21 
22     }
23 
24     /**
25      * request創建時調用
26      */
27     @Override
28     public void requestInitialized(ServletRequestEvent event) {
29         HttpServletRequest request = (HttpServletRequest) event;
30         // 客戶端ip
31         String ip = request.getRemoteAddr();
32         // 訪問的URL地址
33         String url = request.getRequestURI();
34         // 只做簡單后台打印
35         System.out.println("The client ip is " + ip);
36         System.out.println("The address url is " + url);
37     }
38 
39 }
View Code

5.在web.xml中配置隊一行的listener

        <listener>
        <listener-class>
            com.lt.listener.MyContextListener
        </listener-class>
    </listener>
    <listener>
        <listener-class>
            com.lt.listener.MySessionListener
        </listener-class>
    </listener>
    <listener>
        <listener-class>
            com.lt.listener.MyRequestListener
        </listener-class>
    </listener>    
View Code

Listener種類:

  1.監聽對象的創建與銷毀的Listener:

  HttpSessionListener: sessionCreated(HttpSessionEvent sessionEvent)、sessionDestroyed(HttpSessionEvent sessionEvent)

  ServletRequestListener: requestInitialized(ServletRequestEvent event)、requestDestroyed(ServletRequestEvent event)

  ServletContextListener: contextInitialized(ServletContextEvent event)、contextDestroyed(ServletContextEvent event)

  2.監聽對象的屬性變化的Listener:

  HttpSessionAttributeListener:(添加、更新、移除session時觸發)

  attributeAdded(HttpSessionBindingEvent event)、attributeReplaced(HttpSessionBindingEvent event)、attributeRemoved(HttpSessionBindingEvent event)

  ServletContextAttributeListener:(添加、更新、移除context時觸發)

    attributeAdded(ServletContextAttributeEvent event)、attributeReplaced(ServletContextAttributeEvent event)、attributeRemoved(ServletContextAttributeEvent event) 

  ServletRequestAttributeListener:(添加、更新、移除request時觸發)

  attributeAdded(ServletRequestAttributeEvent event)、attributeReplaced(ServletRequestAttributeEvent event)、attributeRemoved(ServletRequestAttributeEvent event) 

  3.監聽Session內的對象

  HttpSessionBindingListener:(對象放入session、對象從session移除時觸發)

  valueBound(HttpSessionBindingEvent event)、valueUnbound(HttpSessionBindingEvent event)

  HttpSessionActivationListener:(session中的對象被鈍化、對象被重新加載時觸發ps:將session中的內容保存到硬盤的過程叫做鈍化,鈍化需實現Serializable序列化接口)

  sessionWillPassivate(HttpSessionEvent event)、sessionDidActivate(HttpSessionEvent event)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM