Use Listener to record the cumulative number of visitors, the maximum number of simultaneous online users, and the number of currently logged-on users.

Source: Internet
Author: User

Use Listener to record the cumulative number of visitors, the maximum number of simultaneous online users, and the number of currently logged-on users.

1. The global statistics variable class of the website. Only global variables are defined.

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 * website global variable class 10 * @ author LIUTIE11 * 12 */13 public abstract class ApplicationConstants {14 15/** 16 * user login session name 17 */18 public static final String LOGIN_SESSION_NAME = "userInfo "; 19 20/** 21 * index all sessions 22 * for single logon 23/24 public static Map <String, HttpSession> SESSION_MAP = new HashMap <> (); 25 26/** 27 * Current online users 28 */29 public static int CURRENT_LOGIN_COUNT = 0; 30 31/** 32 * Total Number of historical visitors 33 */34 public static int TOTAL_HISTORY_COUNT = 0; 35 36/** 37 * Maximum number of simultaneous online users 38 */39 public static int MAX_ONLINE_COUNT = 0; 40 41/** 42 * Server start time 43 */44 public static Date SERVER_START_DATE = new Date (); 45 46/** 47 * Maximum number of online users time 48 */49 public static Date MAX_ONLINE_COUNT_DATE = new Date (); 50 51 52 53}
View Code

2. Implement servletContext listening to record Server Information

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 Listen 10 * record server information such as startup and shutdown time 11 * @ author LIUTIE12 * 13 */14 public class MyContextListener implements ServletContextListener {15 16/** 17 * When the server is started, it is called 18 */19 @ Override20 public void contextDestroyed (ServletContextEvent arg0) {21 // record the startup time 22 ApplicationConstants. SERVER_START_DATE = new Date (); 23} 24 25/** 26 * when the server is shut down, 27 */28 @ Override29 public void contextInitialized (ServletContextEvent arg0) is called) {30 // save data to hard disk 31 // TODO Auto-generated method stub32} 33 34}
View Code

3. Implement HttpSessionListener and HttpSessionAttributeListener listeners to record logon information, total number of visits, and number of online users, and achieve single logon.

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 listening 13 * record the total number of online users for login information 14 * Single login 15 * @ author LIUTIE 16*17 */18 pub Lic class MySessionListener implements HttpSessionListener, HttpSessionAttributeListener {19 20/** 21 * called at session creation 22 */23 @ Override 24 public void sessionCreated (HttpSessionEvent sessionEvent) {25 // obtain the created session 26 HttpSession session = sessionEvent. getSession (); 27 // Add to map 28 ApplicationConstants. SESSION_MAP.put (session. getId (), session); 29 // total number of visits + + 30 ApplicationConstants. TOTAL_HIS TORY_COUNT ++; 31 // if the total number of maps is greater than the maximum number of simultaneous online users, update the maximum number of online users and time 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 * when the session is destroyed, 41 */42 @ Override 43 public void sessionDestroyed (HttpSessionEvent sessio NEvent) {44 // obtain the session to be destroyed 45 HttpSession session = sessionEvent. getSession (); 46 // Delete 47 ApplicationConstants in map based on the key. SESSION_MAP.remove (session. getId (); 48} 49 50/** 51 * called when the session attribute is added 52 */53 @ Override 54 public void attributeAdded (HttpSessionBindingEvent event) {55 // determine whether to add the User Logon Information session 56 if (event. getName (). equals (ApplicationConstants. LOGIN_SESSION_NAME) {57 // number of currently logged on users + + 5 8 ApplicationConstants. CURRENT_LOGIN_COUNT ++; 59 // whether to log on to another machine to process 60 isLoginInOtherPlace (event ); 61} 62} 63 64/** 65 * is called when the session attribute is removed 66 */67 @ Override 68 public void attributeRemoved (HttpSessionBindingEvent event) {69 // determine whether to remove the user logon information session 70 if (event. getName (). equals (ApplicationConstants. LOGIN_SESSION_NAME) {71 // number of currently logged on users -- 72 ApplicationConstants. CURRENT_LOGIN_COUNT --; 73 // whether it is logged on from another machine 74 isLoginInOtherPlace (event); 75} 76} 77 78/** 79*80 */81 @ Override 82 public void attributeReplaced (HttpSessionBindingEvent event) {83 84 // determine whether to modify the user logon information session 85 if (event. getName (). equals (ApplicationConstants. LOGIN_SESSION_NAME) {86 // whether to log on to other machines to process 87 isLoginInOtherPlace (event ); 88} 89} 90 91/** 92 * log on to another machine to process 93*94 * @ param event 95 */96 private void isLogi NInOtherPlace (HttpSessionBindingEvent event) {97 // obtain the added session 98 HttpSession session = event. getSession (); 99 // traverse to find whether the user is logged on to 100 for (HttpSession s: ApplicationConstants. SESSION_MAP.values () {101 // if you have logged on to another machine, it will expire 102 if (event. getValue (). equals (s. getAttribute (ApplicationConstants. LOGIN_SESSION_NAME) 103 & session. getId ()! = S. getId () {104 // invalidate session 105 session. invalidate (); 106 break; 107} 108} 109} 110}
View Code

4. Implement request listening to record the ip addresses and URLs of customer information.

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 listening is used to record client information such as ip address and url 9*10 * @ author LIUTIE11 * 12 */13 public class MyRequestListener implements ServletRequestListener {14 15 /** 16 * Call 17 */18 @ Override19 public void requestDestroyed (ServletRequestEvent event) when the request is destroyed) {20 // TODO Auto-generated method stub21 22} 23 24/*** 25 * Call 26 */27 @ Override28 public void requestInitialized (ServletRequestEvent event) when the request is created) {29 HttpServletRequest request = (HttpServletRequest) event; 30 // client ip31 String ip = request. getRemoteAddr (); 32 // URL for access 33 String url = request. getRequestURI (); 34 // only prints 35 systems in the background. out. println ("The client ip is" + ip); 36 System. out. println ("The address url is" + url); 37} 38 39}
View Code

5. Configure the listener of a line in web. xml.

        <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 type:

1. Listener object creation and destruction Listener:

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

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

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

2. Listener for Listener object attribute change:

HttpSessionAttributeListener: (triggered when a session is added, updated, or removed)

AttributeAdded (HttpSessionBindingEvent event), attributeReplaced (HttpSessionBindingEvent event), attributeRemoved (HttpSessionBindingEvent event)

ServletContextAttributeListener: (triggered when context is added, updated, or removed)

AttributeAdded (ServletContextAttributeEvent event), attributeReplaced (ServletContextAttributeEvent event), attributeRemoved (ServletContextAttributeEvent event)

ServletRequestAttributeListener: (triggered when a request is added, updated, or removed)

AttributeAdded (ServletRequestAttributeEvent event), attributeReplaced (ServletRequestAttributeEvent event), attributeRemoved (ServletRequestAttributeEvent event)

3. Listen to objects in the Session

HttpSessionBindingListener: (triggered when the object is put into the session and the object is removed from the session)

ValueBound (HttpSessionBindingEvent event), valueUnbound (HttpSessionBindingEvent event)

HttpSessionActivationListener: (the object in the session is passive, and the ps is triggered when the object is reloaded. The process of saving the content in the session to the hard disk is called passivation. The Serializable serialization interface must be implemented)

SessionWillPassivate (HttpSessionEvent event), sessionDidActivate (HttpSessionEvent event)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.