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/>
相关推荐
新手小袁_J19 分钟前
JDK11下载安装和配置超详细过程
java·spring cloud·jdk·maven·mybatis·jdk11
呆呆小雅20 分钟前
C#关键字volatile
java·redis·c#
Monly2121 分钟前
Java(若依):修改Tomcat的版本
java·开发语言·tomcat
Ttang2323 分钟前
Tomcat原理(6)——tomcat完整实现
java·tomcat
钱多多_qdd34 分钟前
spring cache源码解析(四)——从@EnableCaching开始来阅读源码
java·spring boot·spring
waicsdn_haha36 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
Q_19284999061 小时前
基于Spring Boot的摄影器材租赁回收系统
java·spring boot·后端
Code_流苏1 小时前
VSCode搭建Java开发环境 2024保姆级安装教程(Java环境搭建+VSCode安装+运行测试+背景图设置)
java·ide·vscode·搭建·java开发环境
禁默2 小时前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
Cachel wood2 小时前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架