網上眾說紛紜,呵,什么記錄文件啊,什么記錄數據庫啊,都能實現,關鍵是效率問題。這里,我用的是我項目中用過的一個工具,非常好用,話不多說,看招:
SessionCounter.java
package com.Gavin.tools.sessioncount;

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.*;
/**
* **********************************************
* @description 記錄在線人數
* @author Gavin.lee
* @date Jun 25, 2009 3:39:34 PM
* @version 1.0
***********************************************
*/
public class SessionCounter extends HttpServlet implements HttpSessionListener {
private static int activeSessions = 1;

public static int getActiveSessions() {
return activeSessions;
}

public void sessionCreated(HttpSessionEvent httpSessionEvent) {
activeSessions++;
}
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
activeSessions--;
}
}
count.jsp
<%@ page import="com.Gavin.tools.sessioncount.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"{
pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Insert title here</title>
</head>

<body >
在線:<%= SessionCounter.getActiveSessions() %>
</body>
</html>
web.xml
<listener>
<listener-class>com.Gavin.tools.sessioncount.SessionCounter</listener-class>
</listener>
SessionCounter.java

































count.jsp














web.xml


