我需要制作持久的和用户特定的会话计数器。我做的
package my.package;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
public class SessionCounter implements HttpSessionListener {
private static HashMap activeSessions;
public SessionCounter() {
//How to restore session count?
activeSessions = new HashMap();
}
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
String userName = session.getAttribute("username");
Integer count = (Integer) activeSessions.get(userName);
if (count != null) {
activeSessions.put(userName, Integer.valueOf(count.intValue() + 1));
} else {
activeSessions.put(userName, new Integer(1));
}
}
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
String userName = session.getAttribute("username");
Integer count = (Integer) activeSessions.get(userName);
if (count != null && count.intValue() > 0) {
activeSessions.put(userName, Integer.valueOf(count.intValue() - 1));
}
}
public static HashMap getActiveSessions() {
return activeSessions;
}
}
即使在重新启动 tomcat 后会话仍处于活动状态,但存储在我的 activeSessions 变量中的会话计数丢失了。重启后如何恢复会话计数?