java統計線上人數
阿新 • • 發佈:2019-02-15
在web.xml檔案中新增
<!-- 統計線上 監聽 --> <listener> <listener-class>com.connxun.config.listener.OnLineListener</listener-class> </listener>
然後新增對應路徑的監聽檔案
package com.connxun.config.listener; import com.connxun.util.session.UserSession; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import java.util.ArrayList; import java.util.HashMap; importjava.util.List; /** * 統計線上人數 * * @author gaoyf */ public class OnLineListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener { // 宣告一個ServletContext物件 private ServletContext application = null; private static HashMap<String, UserSession> hashMap = newHashMap<String, UserSession>(); public void contextInitialized(ServletContextEvent sce) { // 容器初始化時,向application中存放一個空的容器 this.application = sce.getServletContext(); this.application.setAttribute("allUser", new ArrayList<UserSession>()); } public void contextDestroyed(ServletContextEvent sce) { } public void sessionCreated(HttpSessionEvent se) { UserSession userSession = (UserSession) se.getSession().getAttribute("userSession"); if (userSession != null) { hashMap.put(se.getSession().getId(), userSession); System.out.println("使用者登入,當前使用者總數:" + hashMap.size()); } System.out.println(se.getSession().getId()); } public void sessionDestroyed(HttpSessionEvent se) { // 將使用者名稱稱從列表中刪除 List l = (List) this.application.getAttribute("allUser"); UserSession userSession = (UserSession) se.getSession().getAttribute("userSession"); if (userSession != null) l.remove(userSession); System.out.println("session過期,當前使用者數:" + l.size()); this.application.setAttribute("allUser", l); System.out.println(se.getSession().getId()); if (hashMap.containsKey(se.getSession().getId())) { hashMap.remove(se.getSession().getId()); System.out.println("session自動銷燬:" + hashMap.size()); } } public void attributeAdded(HttpSessionBindingEvent se) { // 如果登入成功,則將使用者名稱儲存在列表之中 List<UserSession> l = (List<UserSession>) this.application.getAttribute("allUser"); l.add((UserSession) se.getValue()); this.application.setAttribute("allUser", l); hashMap.put(se.getSession().getId(), (UserSession) se.getValue()); System.out.println(se.getSession().getId()); System.out.println("系統登入,當前使用者數:" + hashMap.size()); UserSession userSession = (UserSession) se.getSession().getAttribute("userSession"); if (userSession != null) { System.out.println(se.getSession().getId()); hashMap.put(se.getSession().getId(), userSession); System.out.println("使用者登入,當前使用者總數:" + hashMap.size()); } } public void attributeRemoved(HttpSessionBindingEvent se) { List<UserSession> l = (List<UserSession>) this.application.getAttribute("allUser"); UserSession userSession = (UserSession) se.getValue(); if (userSession != null) l.remove(userSession); System.out.println(se.getSession().getId()); hashMap.remove(se.getSession().getId()); System.out.println("使用者登出,當前使用者數:" + hashMap.size()); this.application.setAttribute("allUser", l); } public void attributeReplaced(HttpSessionBindingEvent se) { } }