tomcat中不同应用session共享

保存session

java 复制代码
HttpSession session=request.getSession();
				session.setAttribute("logonUser",logonuser );
				//session.setMaxInactiveInterval(15);
				
				String sessionid=session.getId();
				session.setAttribute("sessionid", sessionid);
				
				ServletContext ContextA =request.getSession().getServletContext(); 
				Map<String,HttpSession> sessions = (Map<String, HttpSession>) ContextA.getAttribute("sessions");
				if(sessions ==null){
					sessions = new HashMap<>();
				}
				sessions.put(session.getId(), session);
				ContextA.setAttribute("sessions",sessions);
				setLogonlog(username,request, 1);
				success("登录成功");

取值

java 复制代码
 String sessionid = request.getParameter("sessionid");


        try {
            HttpSession session = request.getSession();
            ServletContext ContextB = session.getServletContext();
            ServletContext ContextA = ContextB.getContext("/WebappA");// 这里面传递的是 WebappA登录应用的虚拟路径
            Map<String, HttpSession> sessions = (Map<String, HttpSession>) ContextA.getAttribute("sessions");
            HttpSession session2 = sessions.get(request.getParameter("sessionid"));

            HashMap<String, Object> logonuser = (HashMap<String, Object>) session2.getAttribute("logonUser");
            System.err.println(logonuser.size());
            for (String key : logonuser.keySet()) {

                System.out.println("Key2: " + key + " Value: " + logonuser.get(key));

            }

            session.setAttribute("logonUser", logonuser);

            session.setAttribute("sessionid", sessionid);


            // 登录账号区分大小写
            User user = userMapper.getByLoginAccountIgnoreCase((String) logonuser.get("userid"));
            user.apply(user);
            SessionManager.create(request).userLogon(user);
        } catch (Exception e) {
            log.error("平台跳转错误。。。。");
            log.error(e.getMessage());
        }

         return "redirect:/index.html";

参考

java 复制代码
WebappA:

HttpSession session = request.getSession();
session.setAttribute("userId", "test");
ServletContext ContextA =session .getServletContext();
ContextA.setAttribute("session", session );
 
WebappB:

HttpSession sessionB = request.getSession();  
ServletContext ContextB = sessionB.getServletContext();  
ServletContext ContextA= ContextB.getContext("/WebappA");// 这里面传递的是 WebappA的虚拟路径
HttpSession sessionA =(HttpSession)ContextA.getAttribute("session");
System.out.println("userId: "+sessionA.getAttribute("userId"));
 `

server.xml

xml 复制代码
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
			

	<Context crossContext="true" docBase="Plat" path="/Plat" reloadable="true" sessionCookiePath="/" />			
 <Context crossContext="true" docBase="SYS" path="/SYSCMS" reloadable="true" sessionCookiePath="/" />		   
 <Context crossContext="true" docBase="sydsue" path="/sydsunew" reloadable="true"  sessionCookiePath="/" />		   
 	   
			   
			   
			   
			   
			   
			   

      </Host>

web.xml

csharp 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
		 <distributable/>
相关推荐
Bs_MoneyMagnet3 小时前
基于springboot+vue的滑雪场票务与装备租赁系统的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring·毕业设计·计算机毕业设计
野生技术架构师8 小时前
2026 Java 面试全套总结,八股 + 场景 + AI 相关面试考点
java·人工智能·面试
香吧香8 小时前
java服务异常日志只打印异常类型,没有堆栈定位分析
java·jvm·异常
qq_2518364579 小时前
AI 疾病自查功能SpringbootAI项目实战 · 技术实现文档
java·ai·ai编程
逆境不可逃9 小时前
Pi Agent 学习笔记:对话太长以后,如何压缩上下文
java
MetaLite9 小时前
JDK 8~26 核心特性一览:从 Stream 到 Scoped Values
java
wno70410 小时前
Spring Boot WebFlux增删改查
java·spring boot·后端
君顾110 小时前
上海24小时自助健身房系统开发实战指南:从架构设计到落地部署
java·开发语言·健身房
SL_staff11 小时前
别急着买商业规则引擎:90%风控场景根本不需要全量编码能力
java·程序员·groovy
ManageEngineITSM12 小时前
DevOps和ITIL是什么关系?是替代还是互补一文讲清
java·服务器·资产管理·变更管理