[100分讨论] 如何判断一个用户是否在线!?

zhaoqiubo 2003-12-12 09:13:42
在每个用户登录以后我都把他的用户信息放到SESSION中了,如何得到在线用户的列表呢?
...全文
98 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhenshangzhengren 2010-06-21
  • 打赏
  • 举报
回复
mark
zhaoqiubo 2003-12-15
  • 打赏
  • 举报
回复
我知道了,用其他方法的朋友们也来说说呀!
yourworking 2003-12-12
  • 打赏
  • 举报
回复
好象有个30分钟期限
   不知如何处理????
hawkhxz 2003-12-12
  • 打赏
  • 举报
回复
关闭IE,立即死掉,其它等待一段时间
wuyg719 2003-12-12
  • 打赏
  • 举报
回复
session是如何死掉和失效的呢?
超时,直接关闭IE,网络断线,客户端死机,这些情况session会自动死掉(失效)?
是立即死掉(失效)还是等待一段时间?
minger214 2003-12-12
  • 打赏
  • 举报
回复
帮你up......
gks_cn 2003-12-12
  • 打赏
  • 举报
回复
package sys.user;

import util.db.DB;
import java.sql.*;
import util.string.StringUtil;
import sun.jdbc.rowset.CachedRowSet;
import javax.servlet.http.*;
import java.util.ArrayList;
import sys.all.SysLockIp;

public class UserSession
implements HttpSessionBindingListener {
private String userName;
private String userId;
private String userType = "";
private String gotoUrl;
private boolean isLogin;
private String password;
public static ArrayList userList = new ArrayList();
public void valueBound(HttpSessionBindingEvent event) {
boolean dup = false;
for (int i = 0; i < this.userList.size(); i++) {
UserSession u = (UserSession)this.userList.get(i);
if (u != null && u.getUserName() != null &&
u.getUserId().equals(this.getUserId())) { // forbid add a duplicate user
dup = true;
}
}
if (!dup) {
this.userList.add(this);
}
System.out.println(this.userId + " added into userlist");
}

public void valueUnbound(HttpSessionBindingEvent event) {
this.userList.remove(this);
System.out.println(this.userId + " remove from userlist");
}


}
junyi2003 2003-12-12
  • 打赏
  • 举报
回复
Mark
chmask 2003-12-12
  • 打赏
  • 举报
回复
感觉在库里加一个字段比较好!
wuyg719 2003-12-12
  • 打赏
  • 举报
回复
在 APPLICATION 里建个 用户数组, 每个用户登陆后 将他的信息 保存到SESSION 和 该数组里,
如果SESSION死掉 就在书组 里找到该信息 并删除
我觉得用个专门的bean比较好。用户登陆的时候在bean里面添加相应信息,session失效的时候从bean里面删除。想显示在线用户列表的话就直接从bean里面读取。

session是如何死掉和失效的呢?
超时,直接关闭IE,网络断线,客户端死机,这些情况session会自动死掉(失效)?
是立即死掉(失效)还是等待一段时间?
hansun 2003-12-12
  • 打赏
  • 举报
回复
关注~~~
zhaoqiubo 2003-12-12
  • 打赏
  • 举报
回复
有个事件监听器,不知道可不可以用到这里呢?
zhaoqiubo 2003-12-12
  • 打赏
  • 举报
回复
由于本人的水平实在有限,拜托yzwkk(流星飞雨)能不能帮我给您的程序加一点注释呢?
  • 打赏
  • 举报
回复
mark
yzwkk 2003-12-12
  • 打赏
  • 举报
回复
同意楼上的说法
以前在论坛中用过的一个方法
public static void checkUserStats(HttpServletRequest request,HttpServletResponse response,String stats){

String userName=GCookie.getCookieValue(request,"UJBBUName","");
String userPassword=GCookie.getCookieValue(request,"UJBBUPSW","");
String ip=request.getRemoteAddr();
String[] addr=IPLocalizer.search(request);
String comeFrom="";
for(int i=0;i<addr.length;i++)
comeFrom+=addr[i];
String actCome=request.getHeader("X_FORWARDED_FOR");
long statUserID=Long.parseLong(StringUtils.replace(ip,".",""));
boolean onlineSign=false;
String browser=request.getHeader("User-Agent");
ResultSet rs;
try{
String sql="";
DBConnect dbc=new DBConnect();
try{
sql="select * from bbs_online where id=?";
dbc.prepareStatement(sql);
dbc.setLong(1,statUserID);
//dbc.setBytes(2,userName.getBytes("GBK"));
rs=dbc.executeQuery();
if(rs.next())
onlineSign=true;
else
onlineSign=false;
rs.close();
}
catch(Exception e){
onlineSign=false;
}





if("".equals(userName)){
if(!onlineSign){
sql="insert into bbs_online (id,username,userclass,ip,startime,lastimebk,browser,stats,actforip,ComeFrom,actCome) values("+statUserID+",?,?,?,?,?,?,?,?,?,?)";
dbc.prepareStatement(sql);
//dbc.setLong(1,statUserID);
dbc.setString(1,"guest");
dbc.setString(2,"客人".getBytes("GBK").toString());
dbc.setString(3,ip.getBytes("GBK").toString());
dbc.setString(4,Format.getDateTime());
dbc.setString(5,Format.getDateTime());
dbc.setString(6,browser);
dbc.setString(7,stats.getBytes("GBK").toString());
if(actCome==null)
{
dbc.setString(8,"null");
dbc.setString(10,"null");
}
else
{
dbc.setString(8,actCome);
dbc.setString(10,actCome);
}
dbc.setString(9,comeFrom.getBytes("GBK").toString());
dbc.executeUpdate();
dbc.clearParameters();

}
else{
sql="update bbs_online set lastimebk=?,lastime=?,ComeFrom=?,actCome=?,stats=? where id="+statUserID;

System.out.println("1"+Format.getDateTime()+"2"+Format.getStrDateTime()+"3"+comeFrom.getBytes("GBK")+"4"+actCome+"5"+stats.getBytes("GBK")+"kill");
dbc.prepareStatement(sql);
dbc.setString(1,Format.getDateTime());
dbc.setString(2,Format.getStrDateTime());
dbc.setString(3,"'"+comeFrom.getBytes("GBK").toString()+"'");
if(actCome==null)
{
dbc.setString(4,"null");
}
else{
dbc.setString(4,actCome);
}
dbc.setString(5,"'"+stats.getBytes("GBK").toString()+"'");
//dbc.setLong(6,statUserID);
dbc.executeUpdate();
sql="update bbs_online set ComeFrom=? where id="+statUserID;
dbc.prepareStatement(sql);
dbc.setString(1,"'"+comeFrom.getBytes("GBK").toString()+"'");
dbc.executeUpdate();
dbc.clearParameters();

}
}
else{
sql="select * from bbs_online where ID="+statUserID+" or username=?";
dbc.prepareStatement(sql);
//dbc.setLong(1,statUserID);
dbc.setBytes(1,userName.getBytes("GBK"));
rs=dbc.executeQuery();
if(rs.next())
onlineSign=true;
else
onlineSign=false;
dbc.clearParameters();
try{
User theUser=new User(userName,userPassword,4);
if(onlineSign){

sql="update bbs_online set id="+statUserID+",userName=?,userClass=?,lastimebk=?,lastime=?,ComeFrom=?,actCome=?,stats=? where id=? or username=?";
dbc.prepareStatement(sql);
//dbc.setLong(1,statUserID);
dbc.setBytes(1,userName.getBytes("GBK"));
dbc.setBytes(2,getUserClass(theUser.getUserClass()).getBytes("GBK"));
dbc.setString(3,Format.getDateTime());
dbc.setString(4,Format.getStrDateTime());
dbc.setBytes(5,comeFrom.getBytes("GBK"));
dbc.setString(6,actCome);
dbc.setBytes(7,stats.getBytes("GBK"));
dbc.setLong(8,statUserID);
dbc.setBytes(9,userName.getBytes("GBK"));
dbc.executeUpdate();
dbc.clearParameters();
}
else{

sql="insert into bbs_online(id,username,userclass,ip,startime,lastimebk,browser,stats,actforip,ComeFrom,actCome) values("+statUserID+",?,?,?,?,?,?,?,?,?,?)";
dbc.prepareStatement(sql);
//dbc.setLong(1,statUserID);//statUserID);
dbc.setString(1,userName.getBytes("GBK").toString());
dbc.setString(2,getUserClass(theUser.getUserClass()).getBytes("GBK").toString());
dbc.setString(3,ip);
dbc.setString(4,Format.getDateTime());
dbc.setString(5,Format.getStrDateTime());
dbc.setString(6,browser);
dbc.setString(7,stats.getBytes("GBK").toString());
System.out.println("tttt"+actCome);
if(actCome==null){
dbc.setString(8,"null");
dbc.setString(10,"null");
}
else{
dbc.setString(8,actCome);
dbc.setString(10,actCome);
}

dbc.setString(9,comeFrom.getBytes("GBK").toString());

dbc.executeUpdate();
dbc.clearParameters();
}
}
catch(Exception e){

if(!onlineSign){
sql="insert into bbs_online (id,username,userclass,ip,startime,lastimebk,browser,stats,actforip,ComeFrom,actCome) values("+statUserID+",?,?,?,?,?,?,?,?,?,?)";
dbc.prepareStatement(sql);
//dbc.setLong(1,statUserID);
dbc.setString(1,"guest");
dbc.setString(2,"客人".getBytes("GBK").toString());
dbc.setString(3,ip.getBytes("GBK").toString());
dbc.setString(4,Format.getDateTime());
dbc.setString(5,Format.getDateTime());
dbc.setString(6,browser);
dbc.setString(7,stats.getBytes("GBK").toString());
dbc.setString(8,actCome);
dbc.setString(9,comeFrom.getBytes("GBK").toString());
dbc.setString(10,actCome);
dbc.executeUpdate();
dbc.clearParameters();

}
else{
sql="update bbs_online set lastimebk=?,lastime=?,ComeFrom=?,actCome=?,stats=? where id="+statUserID;
System.out.println("1"+Format.getDateTime()+"2"+Format.getStrDateTime()+"3"+comeFrom.getBytes("GBK")+"4"+actCome+"5"+stats.getBytes("GBK")+"kill");
dbc.prepareStatement(sql);
dbc.setString(1,Format.getDateTime());
dbc.setString(2,Format.getStrDateTime());
dbc.setString(3,"'"+comeFrom.getBytes("GBK").toString()+"'");
if(actCome==null)
{
dbc.setString(4,"null");
}
else{
dbc.setString(4,actCome);
}

dbc.setString(5,"'"+stats.getBytes("GBK").toString()+"'");
//dbc.setLong(6,statUserID);
dbc.executeUpdate();
dbc.clearParameters();

}

}

}



sql="delete from bbs_online where now()>date_add(lastimebk,interval 20 minute)";

dbc.executeUpdate(sql);
sql="select Maxonline from config";
rs=dbc.executeQuery(sql);
rs.next();
int oldMaxOnLine=rs.getInt(1);
sql="select count(*) from bbs_online";
ResultSet tmprs=dbc.executeQuery(sql);
tmprs.next();
int newMaxOnLine=tmprs.getInt(1);
if(newMaxOnLine>oldMaxOnLine){
sql="update config set Maxonline="+newMaxOnLine+",MaxonlineDate=now()";
dbc.executeUpdate(sql);
ForumPropertiesManager.resetManager();
}

dbc.close();

}
catch(Exception e){

e.printStackTrace();
}

}
flowercat 2003-12-12
  • 打赏
  • 举报
回复
同意楼上的,需要考虑bean的生命周期。也可以考虑用servlet:)推荐
aoenzh 2003-12-12
  • 打赏
  • 举报
回复
session比较好
bluesmile979 2003-12-12
  • 打赏
  • 举报
回复
我觉得用个专门的bean比较好。用户登陆的时候在bean里面添加相应信息,session失效的时候从bean里面删除。想显示在线用户列表的话就直接从bean里面读取。
StevenWSF 2003-12-12
  • 打赏
  • 举报
回复
在 APPLICATION 里建个 用户数组, 每个用户登陆后 将他的信息 保存到SESSION 和 该数组里,
如果SESSION死掉 就在书组 里找到该信息 并删除
yugona 2003-12-12
  • 打赏
  • 举报
回复
用session比较好吧,加个online 你不还是要判断他是否离线了?不是每个用户都会来注销自己的登陆。
加载更多回复(6)

81,092

社区成员

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

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