当前位置: 技术问答>java相关
怎样判断用户是否在线????????
来源: 互联网 发布时间:2015-01-25
本文导语: 我用jsp做了个bbs,用户登陆后,便把用户信息插入到在线用户表中,但我怎样判定用户是否离线,并将其从在线用户表中删除。谢谢 大家! | 登录加入: monitor.put(session,user.getUserName());//user...
我用jsp做了个bbs,用户登陆后,便把用户信息插入到在线用户表中,但我怎样判定用户是否离线,并将其从在线用户表中删除。谢谢 大家!
|
登录加入:
monitor.put(session,user.getUserName());//user是个对象类
UserMap map = new UserMap();
map.setMap(monitor);
session.setAttribute("Binder.object",map);
UserMap是一个实现HttpSessionListener的类:
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.util.*;
public class UserMap implements HttpSessionBindingListener {
private HashMap map;
public void valueBound(HttpSessionBindingEvent event) {
}
public void valueUnbound(HttpSessionBindingEvent event) {
map.remove(event.getSession());
}
public void setMap(HashMap map){
this.map = map;
}
}
退出时jsp:
((HashMap)application.getAttribute("monitor")).remove(session);
session.invalidate();
查询在线用户jsp:
HashMap monitor = ((HashMap)application.getAttribute("monitor"));
Iterator keyIter = monitor.keySet().iterator();
while(keyIter.hasNext()){
HttpSession https = (HttpSession)keyIter.next();
out.println("");
out.println("");
out.println("" + (String)monitor.get(https) + "
");monitor.put(session,user.getUserName());//user是个对象类
UserMap map = new UserMap();
map.setMap(monitor);
session.setAttribute("Binder.object",map);
UserMap是一个实现HttpSessionListener的类:
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.util.*;
public class UserMap implements HttpSessionBindingListener {
private HashMap map;
public void valueBound(HttpSessionBindingEvent event) {
}
public void valueUnbound(HttpSessionBindingEvent event) {
map.remove(event.getSession());
}
public void setMap(HashMap map){
this.map = map;
}
}
退出时jsp:
((HashMap)application.getAttribute("monitor")).remove(session);
session.invalidate();
查询在线用户jsp:
HashMap monitor = ((HashMap)application.getAttribute("monitor"));
Iterator keyIter = monitor.keySet().iterator();
while(keyIter.hasNext()){
HttpSession https = (HttpSession)keyIter.next();
out.println("");
out.println("");
out.println("" + (String)monitor.get(https) + "
out.println("");
....
....
}
|
你可以在页面上定义一个隐藏的iframe来定时刷新.
openDb.executeQuery("update onlineuser set logondate=sysdate where username='"+name+"'");
如果用户在线的话,把logondate记为当前时间
openDb.executeQuery("delete from onlineuser where (logondate+1/360)
openDb.executeQuery("update onlineuser set logondate=sysdate where username='"+name+"'");
如果用户在线的话,把logondate记为当前时间
openDb.executeQuery("delete from onlineuser where (logondate+1/360)