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/>
相关推荐
Halo_tjn6 分钟前
Java 基于分支和循环结构的专项实验
java·开发语言·计算机
洛_尘7 分钟前
Java EE进阶5:Spring IoC&DI
java·spring·java-ee
IT小哥哥呀9 分钟前
Spring Cloud Stream:一次编写,随处运行
java·spring cloud·微服务··后端开发
Kuo-Teng13 分钟前
LeetCode 141. Linked List Cycle
java·算法·leetcode·链表·职场和发展
洛_尘17 分钟前
数据结构--9:反射、枚举以及lambda表达式(了解即可)
java·开发语言·数据结构
青衫码上行24 分钟前
【Java Web学习 | 第12篇】JavaScript(6)DOM
java·开发语言·前端·javascript·学习
TDengine (老段)29 分钟前
TDengine 字符串函数 POSITION 用户手册
android·java·大数据·数据库·物联网·时序数据库·tdengine
弘毅 失败的 mian29 分钟前
C++、Java 还是测试开发?
java·c++·经验分享·笔记·测试开发·技术方向·就业
杜子不疼.32 分钟前
【C++】 set/multiset底层原理与逻辑详解
java·开发语言·c++
q***318935 分钟前
Spring Boot--@PathVariable、@RequestParam、@RequestBody
java·spring boot·后端