public class OnlineListener implements HttpSessionListener,
HttpSessionAttributeListener {
private static List<SessionAndUser> sessions;
static int delS = -1;
static boolean flag = false;
static {
if (sessions == null) {
sessions = Collections
.synchronizedList(new ArrayList<SessionAndUser>());
}
}
public void sessionCreated(HttpSessionEvent hse) {
System.out.println(hse.getSession() + "-" + new Date());
System.out.println(hse.getSession() + "-" + new Date());
}
public void sessionDestroyed(HttpSessionEvent hse) {
System.out.println("-------------sessionDestroyed()-----------");
System.out.println(hse.getSession() + " "
+ new Date(hse.getSession().getLastAccessedTime()));
System.out.println(hse.getSession() + " " + new Date());
}
public void attributeAdded(HttpSessionBindingEvent e) {
System.out.println("-------------*start added*-----------------------"
+ sessions.size());
HttpSession session = e.getSession();
ActionContext ctx = ActionContext.getContext();
boolean newOne = true;
String attrName = e.getName();
// 登录
if (attrName.equals(Constant.USER_NAME)) {
// 检查登录人数
if (sessions.size() >= Constant.USER_LIMIT) {
newOne = false;
ctx.put("timeoutMSG", "serverBusy");
}
String nowUser = (String) e.getValue();
// 遍历所有session,检查是否已经登录,若是则提示已经登录
for (int i = sessions.size() - 1; i >= 0; i--) {
SessionAndUser tem = sessions.get(i);
if (tem.getUserName().equals(nowUser)) {
newOne = false;
ctx.put("timeoutMSG", "beenLoged");// tem.getSession().invalidate();//
// 同账号顶替登录,自动调用remove
break;
}
}
// 新登录帐号添加进账户维护列表
if (newOne) {
SessionAndUser sau = new SessionAndUser();
sau.setUserName(nowUser);
sau.setSession(session);
sau.setSid(session.getId());
sessions.add(sau);
}
}
}
public void attributeRemoved(HttpSessionBindingEvent e)
throws IllegalStateException {
HttpSession session = e.getSession();
System.out
.println("-------------*start Removed*-----------------------"
+ sessions.size());
if (delS > -1) {
if (flag) {
sessions.remove(delS);
flag = false;
}
} else {
// 登录
String attrName = e.getName();
if (attrName.equals(Constant.USER_NAME)) {
String nowUser = (String) e.getValue();
// 遍历所有session
for (int i = sessions.size() - 1; i >= 0; i--) {
SessionAndUser tem = sessions.get(i);
if (tem.getUserName().equals(nowUser)) {
sessions.remove(i);
break;
}
}
}
}
}
public void attributeReplaced(HttpSessionBindingEvent e) {
HttpSession session = e.getSession();
System.out
.println("-------------*start replace*-----------------------"
+ sessions.size());
String attrName = e.getName();
delS = -1;
// 登录
if (attrName.equals(Constant.USER_NAME)) {
// User nowUser = (User) e.getValue();//old value
String nowUser = (String) session.getAttribute(Constant.USER_NAME);// 当前session中的user
// 遍历所有session
for (int i = sessions.size() - 1; i >= 0; i--) {
SessionAndUser tem = sessions.get(i);
if (tem.getUserName().equals(nowUser)
&& !tem.getSid().equals(session.getId())) {
System.out.println("Remove:invalidate 1!");
delS = i;
flag = true;
} else if (tem.getSid().equals(session.getId())) {
tem.setUserName(nowUser);
}
}
if (delS != -1) {
sessions.get(delS).getSession().invalidate();// 失效时自动调用了remove方法。也就会把它从sessions中移除了
}
}
}
}
session.setAttribute(Constant.USER_NAME, operator.getUsername());
ActionContext ctx = ActionContext.getContext();
if("serverBusy".equals(ctx.get("timeoutMSG"))){
ctx.put("timeoutMSG", "服务器繁忙,请稍后再试");
return "jump";
}
if("beenLoged".equals(ctx.get("timeoutMSG"))){
ctx.put("timeoutMSG", "此账户在别处登录");
return "jump";
}
<%@taglib prefix="s" uri="/struts-tags"%> <s:property value="#attr.timeoutMSG" />
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有