session 监听
1.创建一个session容器用来存放所有的用户,不同浏览器对应不用的session,下面用单例模式来创建session容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | package com.limin.framework.util; import java.util.HashMap; import javax.servlet.http.HttpSession; import com.limin.framework.listener.SessionListener; import com.limin.util.Util; /** * 使用单例模式 */ public class SessionContext { private static SessionContext instance; private HashMap<String,HttpSession> sessionMap; private SessionContext() { sessionMap = new HashMap<String,HttpSession>(); } public static SessionContext getInstance() { if (instance == null ) { instance = new SessionContext(); } return instance; } public synchronized void AddSession(HttpSession session) { if (session != null ) { String userId = session.getAttribute( "userId" ).toString(); if (Util.isNotNullorEmpty(userId)){ sessionMap.put(userId,session); } } } public synchronized void DelSession(HttpSession session) { if (session != null ) { String userId = session.getAttribute( "userId" ).toString(); if (Util.isNotNullorEmpty(userId)){ sessionMap.remove(userId); } session.invalidate(); } } public HashMap<String,HttpSession> getSessionMap() { return sessionMap; } public void setMymap(HashMap<String,HttpSession> sessionMap) { this .sessionMap = sessionMap; } public static synchronized void sessionHandlerByCacheMap(HttpSession session){ try { String userId = session.getAttribute( "userId" ).toString(); HttpSession userSession=(HttpSession)SessionListener.sessionContext.getSessionMap().get(userId); if (userSession!= null ){ //注销在线用户 userSession.invalidate(); } //清除在线用户后,更新map,替换map sessionid 对应session更换成新的session SessionListener.sessionContext.getSessionMap().put(userId,session); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
2. 需要实现 HttpSessionListener 接口,创建一个 SessionListener
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.limin.framework.listener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import com.limin.framework.util.SessionContext; public class SessionListener implements HttpSessionListener{ public static SessionContext sessionContext=SessionContext.getInstance(); public void sessionCreated(HttpSessionEvent httpSessionEvent) { sessionContext.AddSession(httpSessionEvent.getSession()); } public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { sessionContext.DelSession(httpSessionEvent.getSession()); } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法