session监听在线人数,只能监听我的电脑的在线人数,比如我登陆用户a,另一台电脑登录用户b,我电脑上显示的登录人数只有一个a。

我是一只程序媛 2017-03-27 02:40:08
如果我电脑同时登陆a和b两个用户,那我电脑监听的在线人数就是a和b两个。怎么才能获取到其他电脑的session呢?
以下是代码:

package com.business.login;

import java.util.Date;
import java.util.HashMap;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import com.business.workflow.SXRecipient;
import com.dhcc.framework.core.IUser;
/**
* session监听
* @author yuelulu
*
*/
public class OnlineCounterListener implements HttpSessionListener {

public static HashMap<String,OnlineUser> online=new HashMap<String,OnlineUser>();

public void sessionCreated(HttpSessionEvent event) {
//raise();
}
//销毁session
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session=event.getSession();
String sessionId=session.getId();
String loginName=(String)session.getAttribute("loginname");
online.remove(loginName);
}
//创建session
public void createdSession(HttpServletRequest request) {
HttpSession session=request.getSession();
if(session!=null)
{
String sessionId=session.getId();
String loginName=(String)session.getAttribute("loginname");
String userName=new SXRecipient((String)session.getAttribute(IUser.USER_ID)).getRecipName();
String userIp=(String)session.getAttribute("iv-remote-address");
String departmentName="";
Vector v=(Vector)request.getSession().getAttribute(IUser.USER_ORG_ID_NAME);
if(v!=null)
{
departmentName=new SXRecipient(v.get(0)).getRecipName();
}
OnlineUser user=new OnlineUser();
user.setLoginName(loginName);
user.setUserIp(userIp);
user.setLoginTime(new Date());
user.setUserName(userName);
user.setDepartmentName(departmentName);
online.put(loginName, user);
}
}
public static int getOnline() {
return online.size();
}
}


package com.dhcc.framework.util;



import java.io.IOException;

import java.io.PrintWriter;

import java.util.HashMap;

import java.util.Map;



import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;



public class LoginServlet extends HttpServlet implements HttpSessionListener{
//用户和Session绑定关系
public static final Map<String,HttpSession> USER_SESSION=new HashMap<String, HttpSession>();
//seeionId和用户的绑定关系
public static final Map<String, String> SESSIONID_USER=new HashMap<String,String>();
//HttpSessionListener接口监听,监听session的销毁和创建事件
public void sessionDestroyed(HttpSessionEvent se) {
String sessionId=se.getSession().getId();
//当前session销毁时删除当前session绑定的用户信息
//同时删除当前session绑定用户的HttpSession
USER_SESSION.remove(SESSIONID_USER.remove(sessionId));
System.out.println("销毁session:"+sessionId);
}

public void sessionCreated(HttpSessionEvent hse) {
String sessionId = hse.getSession().getId();
System.out.println("创建session:"+sessionId);
}
/**
* 用户登录时的处理
* 处理一个账号同时只有一个地方登录的关键
* @param request
*/
public static void userLoginHandle(HttpServletRequest request){
//当前登录的用户
String loginname=request.getParameter("loginname");
//当前sessionId
String sessionId=request.getSession().getId();
//删除当前sessionId绑定的用户,用户--HttpSession
USER_SESSION.remove(SESSIONID_USER.remove(sessionId));
//删除当前登录用户绑定的HttpSession
HttpSession session=USER_SESSION.remove(loginname);
if(session!=null){
SESSIONID_USER.remove(session.getId());
session.removeAttribute("loginname");
session.setAttribute("userMsg", "您的账号已经在另一处登录了,你被迫下线!");
}

}
/**
* 用户登录
*/
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,IOException {
//获取请求命令
request.setCharacterEncoding("gbk");
String servletPath = request.getServletPath();
//System.out.print(servletPath);
String path = request.getContextPath();
String uri = servletPath.substring(1,servletPath.lastIndexOf(".do"));
try{
if("login".equals(uri)){//登录
HttpSession session=request.getSession();

String loginname=Facility.procNull(request.getParameter("loginname"));

//处理用户登录(保持同一时间同一账号只能在一处登录)

userLoginHandle(request);

//添加用户与HttpSession的绑定
USER_SESSION.put(loginname.trim(), session);

//添加sessionId和用户的绑定

SESSIONID_USER.put(session.getId(), loginname);

System.out.println("用户"+loginname+"已上线");

session.removeAttribute("userMsg");

response.sendRedirect(path+"/framework/index.jsp");
//注意:上面的代码如果没有放在try中,将无法传递参数msg

}else if("reLogin".equals(uri)){//重登陆

HttpSession session=request.getSession();

String loginname = (String)session.getAttribute("loginname");

if(session!=null){

USER_SESSION.remove(SESSIONID_USER.remove(session.getId()));

session.invalidate();

}
if(loginname!=null&&!"".equals(loginname))
//用户下线
System.out.println("用户"+loginname+"下线");
//跳转到项目首页
response.sendRedirect(path+"/login.html");

}else if("getUserMsg".equals(uri)){

HttpSession session=request.getSession();

String CONTENT_TYPE = "text/html; charset=GBK";

response.setContentType(CONTENT_TYPE);

PrintWriter out = response.getWriter();

//System.out.println(session.getAttribute("userMsg"));

out.print(session.getAttribute("userMsg"));
}
}catch(Exception e){

e.printStackTrace();
System.out.println("服务器内部出错");
}
}
}
...全文
148 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
co0lfire 2017-03-28
  • 打赏
  • 举报
回复
监听网站在线人数的话保存在ServletContext session保存的是一个浏览器用户的会话
bobolnear 2017-03-28
  • 打赏
  • 举报
回复
1.通过存到 缓存服务器实现 session 共享 2.通过存到 数据库实现session共享
vkqiang 2017-03-27
  • 打赏
  • 举报
回复
只能获取一个服务器吧

81,091

社区成员

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

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