可以采用cookies和sessionid的做法,这里是使用sessionid的做法。
使用HttpSessionListener来监听session的创建和销毁,集群内部的tomcat开启session复制。2个应用都试用HttpSessionListener来监听
web.xml:
<listener>
<listener-class>com.com.ActiveUserListener2</listener-class>
</listener>
<distributable/>
ActiveUserListener2.java
package com.com;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class ActiveUserListener2 implements HttpSessionListener {
private static int sessionCount2 = 0;
private static Map sessionMaps2 = new HashMap(); //存放session的集合类
public void sessionCreated(HttpSessionEvent arg0) {
HttpSession session = arg0.getSession();
String sessionId = session.getId();
System.out.println("Create a session:" + sessionId);
sessionMaps2.put(sessionId, session);
sessionCount2++;
}
public void sessionDestroyed(HttpSessionEvent arg0) {
sessionCount2--;
String sessionId = arg0.getSession().getId();
sessionMaps2.remove(sessionId);//利用会话ID标示特定会话
System.out.println("Destroy a session:" + sessionId);
}
public static int getSessionCount() {
return sessionCount2;
}
public static Map getSessionMaps() {
return sessionMaps2;
}
}
传递sessionid,?jsessionid=<%=session.getId()%>
测试效果:
<%
Map activeSessions = ActiveUserListener2.getSessionMaps();
out.println(activeSessions.get("CB55ABC39DD5B917D65F456C28FC25E6.tomcat1"));
out.println(activeSessions);
}
}
%>
posted on 2009-02-13 16:46
lzj520 阅读(618)
评论(0) 编辑 收藏 所属分类:
个人学习日记