sessionListener 实现解决一个账号不同时在线需求。
来源:互联网 发布:2012年网络炒作事件 编辑:程序博客网 时间:2024/06/01 09:40
/** * @Project: 无线定位 * @Title: OnLineOnlyOfUserListener.java * @Package: com.wifi.commons.web.listener * @Description: TODO * @Author: chenxu@sina.cn * @Date: 2015年7月20日 上午10:21:07 * @Copyright: 2015 dreamtech.com.cn Inc. All right reserved * @Version: V1.0 */package com.wifi.commons.web.listener;import java.util.Hashtable;import java.util.Map;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * @Classname: OnLineOnlyOfUserListener * @Description: 用于监听用户账号唯一在线监听(一个账号不能同时在线) * @Author: chenxu@sina.cn * @Date: 2015年7月20日 上午10:21:07 * @Version:V1.0 */public class OnLineOnlyOfUserListener implements HttpSessionListener{ private static final Log log = LogFactory.getLog(OnLineOnlyOfUserListener.class); /** * @Title: * @Description: */ public OnLineOnlyOfUserListener() { super(); } /** * 线程安全的集合 * Map<userName,SessionObj> */ private static Map<String, HttpSession> onLine = new Hashtable<String, HttpSession>(); /** * Session 摧毁时 */ @Override public void sessionDestroyed(HttpSessionEvent seEvent) { final String sessionId = seEvent.getSession().getId(); //遍历 for (String key : onLine.keySet()) { if (onLine.get(key).getId().equals(sessionId)) { //log log.info("账号:"+key+"注销,退出系统。"); //从集合中移除 onLine.remove(key); } } } /** * 登录 线程同步的 * @Methodname: login * @Discription: TODO * @return true 登录成功 false 账号已登录 * */ public synchronized static Boolean login(HttpSession session,String userName,Boolean isCompulsoryLogin){ //获取sessionId String sessionId = session.getId(); //判断是否存在SessionId是否已经登录其他用户 如登录则移除 for (String key : onLine.keySet()) { if (onLine.get(key).getId().equals(sessionId)&&key!=userName) { onLine.remove(key); } } //是否已登录 且不是同一个Session if (onLine.containsKey(userName)&&!onLine.get(userName).getId().equals(sessionId)) { //是否强制登录 if (isCompulsoryLogin!=null&&isCompulsoryLogin) { try { //摧毁Session onLine.get(userName).invalidate(); } catch (IllegalStateException e) { log.info(e); } //替换Session onLine.put(userName, session); log.info("账号:"+userName+"被强制登录!"); return true; } log.info("账号:"+userName+"已登录!"); //替换Session return false; } //log log.info("账号:"+userName+"登录成功!"); onLine.put(userName, session); return true; } /** * Session对象创建时 */ @Override public void sessionCreated(HttpSessionEvent se) { // TODO Auto-generated method stub } }
0 0
- sessionListener 实现解决一个账号不同时在线需求。
- Java 实现一个账号不能同时在线
- 用SessionListener实现统计在线人数及访问量
- 用SessionListener实现统计在线人数及访问量
- 实现网页同时只能有一个账号登录
- 一个账号不能同时登陆
- 判断用户是否在线及实现一个账号一个人登陆
- 使用SessionListener统计在线人数
- 使用SessionListener统计在线人数
- 使用SessionListener统计在线人数
- Session、SessionListener、在线人数统计
- 一个很简单的demo来演示一个账号只能同时被一个人使用(Java实现)
- Asp.net 实现只能允许一个账号同时只能在一个地方登录
- JavaWeb中的Session、SessionListener、在线人数统计
- JavaWeb中的Session、SessionListener、在线人数统计
- JavaWeb中的Session、SessionListener、在线人数统计
- JavaWeb中的Session、SessionListener、在线人数统计
- JavaWeb中的Session、SessionListener、在线人数统计
- SolrJ、Request Handler
- ASP.NET 4.0 安裝在 IIS6 最常遇到的四個問題
- MapReduce: 提高MapReduce性能的建议(1)
- 六、Solr高亮与Field权重
- poj 1679 The Unique MST
- sessionListener 实现解决一个账号不同时在线需求。
- JSON详解
- NSInvocation简单使用
- 五种 JSP页面跳转方法详解
- 文章标题
- 并发编程之GCD(来自Xcode Dev 亮了的原创开发技术博客)
- 七、Solr服务部署和安全
- linux之return和exit引发的大问题(vfork和fork)
- 2012年5月SAT香港真题解析