利用session监听程序统计在线人数的一点心得.

flylyke 2004-01-18 12:33:27
这几天在作一个聊天室人数统计的功能.这段时间得到csdn上很多朋友的帮助,现在把这几天遇到的问题和一点心得与大家分享.我是个菜鸟,如果有什么观点不对请尽管批评和指正!
servlet2.3
session监听程序:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class listener extends HttpServlet implements HttpSessionListener, HttpSessionAttributeListener {
private static int sessionCounter=0;
private static int attributeCounter=0;

public void sessionCreated(HttpSessionEvent se) {
sessionCounter++;
System.out.println("session created ok!");
}

public void sessionDestroyed(HttpSessionEvent se) {
sessionCounter--;
System.out.println("session is lost!!");
}

public void attributeAdded(HttpSessionBindingEvent se) {
attributeCounter++;
}
public void attributeRemoved(HttpSessionBindingEvent se) {
attributeCounter--;
}

public void attributeReplaced(HttpSessionBindingEvent se) {
//System.out.println(se.getName()+":is replaced");
}
public static int getSessionCount(){
//System.out.println(sessionCounter);
return sessionCounter;
}
}
统计人数页面:
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="cn.sports.control.*"%>
<html>
<head>
<meta http-equiv =refresh content= "10">
<title>在线人数</title>
<style type="text/css">
<!--
body,td,th {
font-size: 9pt;
color: #000000;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
</head>
<%
int count=listener.getSessionCount();
%>
<body bgcolor="#ffffff">
<table width="100%" height="24" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>当前在线人数:<%=count%></td>
</tr>
</table>
</body>
</html>
以上程序就能根据session的创建或者销毁统计到在线人数.
还有就是要判断当用户非正常退出的时候session的判断.
当用户关闭浏览器窗口是需要触发session.invalidate()事件.
///////////////
<script language=javascript>
function onUnload()
{
if (event.clientX <0 && event.clientY < 0){
window.open('remove.jsp');
}
}
</script>
<body bgcolor="#ffffff" onunload="onUnload()">
///////////////
removesession.jsp
<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>
remove
</title>
</head>
<body bgcolor="#ffffff">
<%
session.invalidate();
%>
</body>
</html>
...全文
196 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
moumourainbow 2004-04-04
  • 打赏
  • 举报
回复
如果不用servlet 光用jsp能实现这种监听功能吗/
最近我用jsp做了个聊天室,没用用servlet,
所以在处理用户非正常退出聊天室(如直接关掉浏览器)时就找不到什么好方法
请高手们办帮帮忙,想想办法
Raulgodle 2004-04-04
  • 打赏
  • 举报
回复
thank U!
wangjinbao 2004-04-04
  • 打赏
  • 举报
回复
好样的
ioly001 2004-03-24
  • 打赏
  • 举报
回复
刚好要做个session listener, 就看到楼主的文章,up!!!
  • 打赏
  • 举报
回复
不错,学习!!!
computersim 2004-01-26
  • 打赏
  • 举报
回复
up
jndszl 2004-01-25
  • 打赏
  • 举报
回复
up
Lauxy 2004-01-25
  • 打赏
  • 举报
回复
up
yan0507 2004-01-24
  • 打赏
  • 举报
回复
up
raymond323 2004-01-24
  • 打赏
  • 举报
回复
up
raymond323 2004-01-24
  • 打赏
  • 举报
回复
up
gjdbf 2004-01-20
  • 打赏
  • 举报
回复
谢谢
flylyke 2004-01-19
  • 打赏
  • 举报
回复
呵呵.这个忘记写上去了
<listener>
<listener-class>cn.sports.control.listener</listener-class>
</listener>
flylyke 2004-01-18
  • 打赏
  • 举报
回复
嗯.是要考虑同步.
还有什么不对的地方请大家继续指正
fantasyCoder 2004-01-18
  • 打赏
  • 举报
回复
绝对要同步!
wlnet 2004-01-18
  • 打赏
  • 举报
回复
计数器是不是还应该考虑同步问题!
fantasyCoder 2004-01-18
  • 打赏
  • 举报
回复
监听器还要在
web.xml文件下
配置!!!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧