您好,登錄后才能下訂單哦!
本文實例為大家分享了Javaweb統計在線人數示的具體代碼,供大家參考,具體內容如下
1. 實現功能
統計在線人數
顯示每個人的sessionId、Ip、第一次登錄時間
2. 運用監聽器種類
3. 實現思想
4. 包目錄
5. 代碼實現
User
package entity; public class User { private String sessionID; private String ip; private String firstTime; public String getSessionID() { return sessionID; } public void setSessionID(String sessionID) { this.sessionID = sessionID; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getFirstTime() { return firstTime; } public void setFirstTime(String firstTime) { this.firstTime = firstTime; } }
MyHttpSessionListener
package listenner; import java.util.ArrayList; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import entity.User; import util.SessionUtil; @WebListener public class MyHttpSessionListener implements HttpSessionListener { private int num = 0;//統計人數 public void sessionCreated(HttpSessionEvent se) { num++; se.getSession().getServletContext().setAttribute("number", num); } @SuppressWarnings("unchecked") public void sessionDestroyed(HttpSessionEvent se) { num--; se.getSession().getServletContext().setAttribute("number", num); //在此用戶被銷毀的時候,將鏈表中對應的用戶對象刪除 ArrayList<User> userlist = (ArrayList<User>) se.getSession().getServletContext().getAttribute("userlist"); if (SessionUtil.getUserBySessionID(userlist, se.getSession().getId()) != null) { userlist.remove(SessionUtil.getUserBySessionID(userlist, se.getSession().getId())); } } }
MyServeltRequestListener
package listenner; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpServletRequest; import entity.User; import util.SessionUtil; @WebListener public class MyServeltRequestListener implements ServletRequestListener { @SuppressWarnings("unchecked") public void requestInitialized(ServletRequestEvent sre) { ArrayList<User> userlist=null; //獲取全局變量中的鏈表,若不存在,則創建一個新的鏈表 userlist= (ArrayList<User>) sre.getServletContext().getAttribute("userlist"); if (userlist == null) { userlist = new ArrayList<User>(); } //獲取request對象 HttpServletRequest request = (HttpServletRequest) sre.getServletRequest(); //獲取sessionID String sessionID = request.getSession().getId(); //通過sessionID在鏈表中查找對象,若沒有此對象,則創建加入到鏈表中 if (SessionUtil.getUserBySessionID(userlist, sessionID) == null) { User user = new User(); user.setSessionID(sessionID); user.setFirstTime(new SimpleDateFormat("YYYY-MM-DD hh:mm:ss").format(new Date())); user.setIp(request.getRemoteAddr()); userlist.add(user); } //將鏈表設置為全局變量 sre.getServletContext().setAttribute("userlist", userlist); } }
getUserBySessionID
package util; import java.util.ArrayList; import entity.User; public class SessionUtil { //工具類,通過session在鏈表中找到相應的用戶對象 public static User getUserBySessionID(ArrayList<User> userlist, String sessionID) { for (int i = 0; i < userlist.size(); i++) { User user=userlist.get(i); if (user.getSessionID().equals(sessionID)){ return user; } } return null; } }
index.jsp
<%@page import="entity.User"%> <%@page import="java.util.ArrayList"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> </head> <body> 當前在線人數:${number} <hr> <% ArrayList<User> userlist = (ArrayList<User>) request.getServletContext().getAttribute("userlist"); if (userlist != null) { for (int i = 0; i < userlist.size(); i++) { User user = userlist.get(i); %> sessionID:<%=user.getSessionID()%> <br> IP:<%=user.getIp()%> <br> FirstName:<%=user.getFirstTime()%> <hr> <%}}%> </body> </html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。