//java 代碼
public class SessionCounter implements HttpSessionListener { private static int activeSessions = 0; //session創建時執行 public void sessionCreated(HttpSessionEvent se) { activeSessions++; } //session銷毀時執行 public void sessionDestroyed(HttpSessionEvent se) { if (activeSessions > 0) activeSessions--; } //獲取活動的session個數(在線人數) public static int getActiveSessions() { return activeSessions; } }
//web.xml添加配置
<listener>
<listener-class>com.java.webutils.SessionListener</listener-class>
</listener>
//jsp頁面編寫
<%@ page import="com.my.count.SessionCounter"%>
<body>
在線人數為:<%=SessionCounter.getActiveSessions()%>
</body>