public class MySessionContext {
private static HashMap mymap = new HashMap();
public static synchronized void AddSession(HttpSession session) {
if (session != null) {
mymap.put(session.getId(), session);
}
}
public static synchronized void DelSession(HttpSession session) {
if (session != null) {
mymap.remove(session.getId());
}
}
public static synchronized HttpSession getSession(String session_id) {
if (session_id == null)
return null;
return (HttpSession) mymap.get(session_id);
}
}
第二步:
public class MySessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
MySessionContext.AddSession(httpSessionEvent.getSession());
}
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
MySessionContext.DelSession(session);
}
}
第三步:
<listener>
<listener-class>com.tr.MySessionListener</listener-class>
</listener>
自定义会话监听器实现
本文介绍了一种自定义的会话监听器实现方法,通过创建MySessionContext类管理和维护活动的HTTP会话,同时利用MySessionListener类对接收到的新会话进行添加并跟踪已结束的会话。
851

被折叠的 条评论
为什么被折叠?



