在web.xml配置文件中:
<listener>
<listener-class>org.util.SessionMap</listener-class>
</listener>
监听类:
package org.util;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class SessionMap implements HttpSessionListener{
private static int sessionCount = 0;
private static Map sessionMaps = new HashMap();
@Override
public void sessionCreated(HttpSessionEvent arg) {
HttpSession session = arg.getSession();
String sessionId = session.getId();
sessionMaps.put(sessionId, session);
sessionCount++;
System.out.println("创建session,当前总数量:"+sessionCount);
Iterator iter = sessionMaps.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry me = (Map.Entry) iter.next();
System.out.println(me.getKey() + "/" + me.getValue());
}
}
@Override
public void sessionDestroyed(HttpSessionEvent arg) {
sessionCount--;
sessionMaps.remove(arg.getSession().getId());
System.err.println("销毁session,当前总数量:"+sessionCount);
Iterator iter = sessionMaps.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry me = (Map.Entry) iter.next();
System.err.println(me.getKey() + "/" + me.getValue());
}
}
public static int getSessionCount() {
return sessionCount;
}
public static Map getSessionMaps() {
return sessionMaps;
}
}
Cookie[] cookies = this.getRequest().getCookies(); for(Cookie c : cookies) { System.err.println("cookie:"+c.getName()+"|"+c.getValue()); } Enumeration ens = this.getSession().getAttributeNames(); while(ens.hasMoreElements()) { System.err.println("session:"+ens.nextElement()+"|" +this.getSession().getAttribute((String) ens.nextElement())); }