统计页面当前在线人数(上下线),请高手指教
我写了一个在线人数统计,如今出现两情状况
状况一:上线时必须手动刷新一次才会进行当前人数统计
状况二:下线时,只有关闭服务器里面的页面才会 -1,关闭IE,火狐,谷歌等浏览器都不会-1;
以下是本人代码,请高手指教:
web.xml//配置文件
   <listener>
   	<listener-class>com.fx.Listener.listener</listener-class>
   </listener>
... servlet配置代码省略 ...
listener.java//监听器
public class listener implements HttpSessionListener {
	private static int count;
	public void sessionCreated(HttpSessionEvent arg0) {
		add();
	}
	public void sessionDestroyed(HttpSessionEvent arg0) {
		del();
	}
	public static void add(){
		++ count;	}
	public static void del(){
		-- count;  
	}
	public static int getCount(){
		return count;
	}
}
servlet.jsp //打开页面时调用的servlet----->doPost方法体
		int count = listener.getCount();
		req.getSession().setAttribute("num", count);
		req.getRequestDispatcher("index.jsp").forward(req, resp);
index.jsp//显示页面
	<script type="text/javascript">
		function window.onunload(){
			if((window.screenLeft>=10000&&window.screenTop>=10000)||event.altKey){
				form1.action="servlet2";
				form1.submit();
			}
		}
	</script>
     <form name="form1">
	    当前在线人数为:${num }
   </form>
servlet2.jsp//关闭页面时调用的servlet----->doPost方法体
		req.getSession().invalidate();
------解决方案--------------------
第一个问题呢,在调用:
int count = listener.getCount();
之前,先去访问下session,确保当前会话已经创建。
第二个问题呢,你需要脑补下会话的含义及其生命周期是啥。
关闭页面不代表会话结束(sessionDestroyed),会话结束只有两种情况:
1、代码中主动调用session.destory(); 这种一般用于 logout 页面;
2、会话超时,也就是用户在很长一段时间没有再次访问该应用,比如半小时,那么中间件会回收该会话。
------解决方案--------------------