日期:2014-05-20  浏览次数:20718 次

jsp怎样解决一个用户异地同时登陆
新手~ 请各位大虾能说清楚一点 谢谢鸟

------解决方案--------------------
探讨
加个字段,登录为1,不登录为0

------解决方案--------------------
写个 在线用户列表,

每次登陆之前 去查询这个列表
------解决方案--------------------
package com.huina.sysframework.listener;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import com.heating.application.agent.common.Constants;
import com.heating.application.agent.common.DateUtils;
import com.heating.application.agent.common.ExceptionStackInfo;
import com.heating.application.agent.common.IpAddress;
import com.heating.application.agent.common.MyLogger;
import com.heating.application.agent.common.ServiceManager;
import com.heating.application.agent.dto.extend.pub.SessionInfo;
import com.heating.application.agent.dto.extend.user.GeneralUserDto;

/**
 * 单点登陆
 * @author Administrator
 */
public class OnLineSessionListener implements HttpSessionAttributeListener {
private Map<String, Object> userMap = new HashMap<String, Object>();

@Override
public void attributeAdded(HttpSessionBindingEvent arg0) {
HttpSession session = arg0.getSession();
//获取用户信息
GeneralUserDto userDto = ((SessionInfo) session.getAttribute("sessionInfo")).getGeneralUserDto();
//用SESSION的唯一ID做KEY。将用户放入
userMap.put(session.getId(), userDto);
// 把用户表是否登陆字段改为1
userDto.setIsLogin(Constants.USER_ISLOGIN_LOGIN);
try {
ServiceManager.user.getUserManagerService().updateGeneralUserDto(
userDto);
} catch (Throwable e) {
MyLogger.getLoggerByName("errorLogger").error(ExceptionStackInfo.getExceptionInfo(e));
}
// 写登陆日志
setUserLoginLogger(userDto);
System.out.println(userDto.getUserName() + "进入系统");
System.out.println("___________________________________");
}

@Override
public void attributeRemoved(HttpSessionBindingEvent arg0) {
HttpSession session = arg0.getSession();
// 获取SESSION的ID
String key = session.getId();
// 获取移除的用户
GeneralUserDto userDto = (GeneralUserDto) userMap.get(key);
try {
userDto = ServiceManager.user.getUserManagerService().
loadGeneralUserDto(userDto.getUserId());
// 把用户表是否登陆字段改为0
userDto.setIsLogin(Constants.USER_ISLOGIN_NOLOGIN);
ServiceManager.user.getUserManagerService().updateGeneralUserDto(
userDto);
} catch (Throwable e) {
MyLogger.getLoggerByName("errorLogger").error(ExceptionStackInfo.getExceptionInfo(e));
}
// 写离开日志
setUserLogoutLogger(userDto);
System.out.println(userDto.getUserName() + "离开系统");
}

@Override
public void attributeReplaced(HttpSessionBindingEvent se) {

}

// 登陆日志
private void setUserLoginLogger(GeneralUserDto userDto){
MyLogger.getLoggerByName("loginLogger").info(
userDto.getUserName() + "在"
+ DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")
+ "登陆系统,Ip地址为:" + IpAddress.getIpAddr());
}

// 离开日志
private void setUserLogoutLogger(GeneralUserDto userDto){
MyLogger.getLoggerByName("loginLogger").info(
userDto.getUserName() + "在"
+ DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")
+ "离开系统");
}
}

------解决方案--------------------