现在有几种办法来保存在线人员:
1、在线人员存放在application中
2、存放在数据库或者xml、properties文件中用户上线,就将其信息保存。难度在于怎么统计用户下线?因为http协议是无连接的,所以要做到实时统计是很难做到的。用户注销肯定不行,用户可能直接关掉IE,也可能很长时间没有任何操作,会话已经过期。
关于用户是否下线,有以下几个设计思路:
1、用户iframe不停刷新自己,然后用session是否过期来判断用户是否在
2、可以使用session超时来控制,但是这样不能准确得到用户的离线时间
3、考虑cookie登录
4、既然做不到实时统计,可以进行一段时间内的人员统计这些在线人员统计的设计思路好像都可行,又好像不行。大家在自己的项目中是怎么解决的?能否分享一下经验?谢谢!
1、在线人员存放在application中
2、存放在数据库或者xml、properties文件中用户上线,就将其信息保存。难度在于怎么统计用户下线?因为http协议是无连接的,所以要做到实时统计是很难做到的。用户注销肯定不行,用户可能直接关掉IE,也可能很长时间没有任何操作,会话已经过期。
关于用户是否下线,有以下几个设计思路:
1、用户iframe不停刷新自己,然后用session是否过期来判断用户是否在
2、可以使用session超时来控制,但是这样不能准确得到用户的离线时间
3、考虑cookie登录
4、既然做不到实时统计,可以进行一段时间内的人员统计这些在线人员统计的设计思路好像都可行,又好像不行。大家在自己的项目中是怎么解决的?能否分享一下经验?谢谢!
楼主的问题很难弄..其实很少有把在线这个做的很准确的..一般都会有一个延迟..不断刷新 定时刷新的话对服务器增加压力了..
我现在做的是搞了个监听器..
不过在集群情况下无效..分别统计各个子服务端的人数..
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class servlis extends HttpServlet implements ServletContextListener, ServletContextAttributeListener,
HttpSessionListener, HttpSessionAttributeListener {
//Notification that the web application is ready to process requests
private static int activeSessions = 1;
public void contextInitialized(ServletContextEvent sce) {
}
//Notification that a session was created
public void sessionCreated(HttpSessionEvent se) {
System.out.println("新创建的session Id 为:"+se.getSession().getId());
activeSessions++; //每创建一个新的session就 加1
}
//Notification that a session was invalidated
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("释放的session Id 为:"+se.getSession().getId());
if (activeSessions > 1) { //每销毁一个新的session就 减1,既然有人触发这个事件
activeSessions--;
}
}
public static int getActiveSessions() {
return activeSessions;//获得当前在线人数
}
}
web.xml里的配置如下:
<listener>
<listener-class>beanservlettest.sessioncount</listener-class>
</listener>
jsp测试页面:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="beanservlettest.sessioncount" %>
<html>
<head>
<title>
sc
</title>
</head>
<body bgcolor="#ffffff">
<h1>
JBuilder Generated JSP
</h1>
当前在线人数有<%=sessioncount.getActiveSessions()%>人。
</body>
</html>
<a href= "logout.jsp">注销</a>
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.sql.*;public class CountListener implements HttpSessionListener { private static long onlineNumber=0;
public void sessionCreated(HttpSessionEvent arg0) {
onlineNumber++;
arg0.getSession().setMaxInactiveInterval(60); } public void sessionDestroyed(HttpSessionEvent arg0) {
onlineNumber--;
if(onlineNumber<=0) onlineNumber=1;
String username=(String)arg0.getSession().getAttribute("username");
if(username!=null&&!username.trim().equals("")){
long ctime=arg0.getSession().getCreationTime();
long dtime=arg0.getSession().getLastAccessedTime();
long online=(dtime-ctime)/1000;
Connection conn=DB.getConn();
Statement stmt=DB.getStmt(conn);
try {
stmt.executeUpdate("update users set onlinetime=onlinetime+"+online+" where username='"+username+"'");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DB.close(stmt);
DB.close(conn);
}
}
public static long getOnlineNumber(){
return onlineNumber;
}}以前做的一个例子,统计在线人数和在线时间
sesion会话过期,浏览器关闭,或直接断电等都算是一个大概统计了
毕竟不是实时通讯的