本文實(shí)例為大家分享了Javaweb統(tǒng)計(jì)在線人數(shù)示的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)建站是一家專業(yè)提供瑞金企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站制作、成都網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為瑞金眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。
1. 實(shí)現(xiàn)功能
統(tǒng)計(jì)在線人數(shù)
顯示每個(gè)人的sessionId、Ip、第一次登錄時(shí)間
2. 運(yùn)用監(jiān)聽(tīng)器種類
3. 實(shí)現(xiàn)思想
4. 包目錄
5. 代碼實(shí)現(xiàn)
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;//統(tǒng)計(jì)人數(shù) 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); //在此用戶被銷毀的時(shí)候,將鏈表中對(duì)應(yīng)的用戶對(duì)象刪除 ArrayListuserlist = (ArrayList ) 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) { ArrayListuserlist=null; //獲取全局變量中的鏈表,若不存在,則創(chuàng)建一個(gè)新的鏈表 userlist= (ArrayList ) sre.getServletContext().getAttribute("userlist"); if (userlist == null) { userlist = new ArrayList (); } //獲取request對(duì)象 HttpServletRequest request = (HttpServletRequest) sre.getServletRequest(); //獲取sessionID String sessionID = request.getSession().getId(); //通過(guò)sessionID在鏈表中查找對(duì)象,若沒(méi)有此對(duì)象,則創(chuàng)建加入到鏈表中 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); } //將鏈表設(shè)置為全局變量 sre.getServletContext().setAttribute("userlist", userlist); } }
getUserBySessionID
package util; import java.util.ArrayList; import entity.User; public class SessionUtil { //工具類,通過(guò)session在鏈表中找到相應(yīng)的用戶對(duì)象 public static User getUserBySessionID(ArrayListuserlist, 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"%>當(dāng)前在線人數(shù):${number}
<% ArrayListuserlist = (ArrayList ) request.getServletContext().getAttribute("userlist"); if (userlist != null) { for (int i = 0; i < userlist.size(); i++) { User user = userlist.get(i); %> sessionID:<%=user.getSessionID()%>
IP:<%=user.getIp()%>
FirstName:<%=user.getFirstTime()%>
<%}}%>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。