|
在web.xml中修改
<listener>
<listener-class>org.eleaf.qsls.server.ActiveUserListener</listener-class>
</listener>
ActiveUserListener.java:
package org.eleaf.qsls.server;
import java.util.*;
import javax.Servlet.http.*;
import org.eleaf.qsls.bbs.*;
public class ActiveUserListener implements HttpSessionListener {
private static int sessionCount = 0;
private static Map sessionMaps = new HashMap(); //存放session的集合类
public void sessionCreated(HttpSessionEvent arg0) {
HttpSession session = arg0.getSession();
String sessionId = session.getId();
System.out.println("Create a session:" + sessionId);
sessionMaps.put(sessionId, session);
sessionCount++;
}
public void sessionDestroyed(HttpSessionEvent arg0) {
sessionCount--;
String sessionId = arg0.getSession().getId();
sessionMaps.remove(sessionId);//利用会话ID标示特定会话
System.out.println("Destroy a session:" + sessionId);
}
public static int getSessionCount() {
return sessionCount;
}
public static Map getSessionMaps() {
return sessionMaps;
}
} |
|