tomcat中不同应用session共享

保存session

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("登录成功");

取值

 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";

参考

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

        <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

<?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/>
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值