星空网 > 软件开发 > Java

tomcat session共享: 同tomcat不同项目之间的session共享

有时我们会遇到一个大点的项目,为了方便实现有些功能,我们会把项目拆成不同的独立web项目。
但我们在管理这些项目时,只有一个登陆口,然后在其他项目取session来实现身份的验证。
查看tomcat 关于 HTTP Connector 中有个emptySessionPath 其解释如下:
If set to true, all paths for session cookies will be set to /. This can be useful for portlet specification implementations. If not specified, this attribute is set to false. A side effect to setting this to true, is that if Tomcat creates a new session it will attempt to use the cookie session id if supplied by the client.
所以的需要一个tomcat下两个WEB之间通过session 共享数据
由于每个WEB应用程序都有一个唯一的一个ServletContext 实例对象,自己下面的所有的servlet 共享此ServletContext。
利用ServletContext 中的setAttribute() 方法把Session传递过去 然后在另外一个WEB程序中拿到session实例。
1: 修改Tomcat---conf----server. tomcat session共享: 同tomcat不同项目之间的session共享tomcat session共享: 同tomcat不同项目之间的session共享

  1.  <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" "false" x  mlValidation="false"></Host>   
  2.     
  3.  修改为:  
  4. <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" "false" x  mlValidation="false">  
  5.    <Context path="/项目A" reloadable="false" crossContext="true"></Context>   
  6.     <Context path="/项目B" reloadable="false" crossContext="true"></Context>  
  7.  </Host>  

 <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" 

注意 crossContext 属性在帮助文档中意思
crossContext: Set to true if you want calls within this application to ServletContext.getContext() to successfully return a request dispatcher for other web applications running on this virtual host. Set to false (the default) in security conscious environments, to make getContext() always return null.
设置为true 说明你可以调用另外一个WEB应用程序 通过ServletContext.getContext() 获得ServletContext 然后再调用其getattribute() 得到你要的对象.

2:  在项目A中,写入以下代码:
我们假定
项目A 为/myweb
项目B为 /w2
//以下内容用于测试同一tomcat下不同项目之间共享session  

Java代码 tomcat session共享: 同tomcat不同项目之间的session共享 tomcat session共享: 同tomcat不同项目之间的session共享tomcat session共享: 同tomcat不同项目之间的session共享

  1. HttpSession session = req.getSession();      
  2. session.setAttribute("name", "xbkaishui");  
  3. session.setMaxInactiveInterval(6565);  
  4. ServletContext ContextA =req.getSession().getServletContext();  
  5. ContextA.setAttribute("session", req.getSession());  

  HttpSession session = req.getSession();    session.setAttribute("name", "xbkaishui");  session.setMaxInactiveInterval(6565);  ServletContext ContextA =req.getSession().getServletContext();  ContextA.setAttribute("session", req.getSession());

//测试 

Java代码 tomcat session共享: 同tomcat不同项目之间的session共享 tomcat session共享: 同tomcat不同项目之间的session共享tomcat session共享: 同tomcat不同项目之间的session共享

  1. out.println("IN SessionRangleServlet name : "+session.getAttribute("name"));  

  out.println("IN SessionRangleServlet name : "+session.getAttribute("name"));

3.在项目B中,写入以下代码取出Session 

Java代码 tomcat session共享: 同tomcat不同项目之间的session共享 tomcat session共享: 同tomcat不同项目之间的session共享tomcat session共享: 同tomcat不同项目之间的session共享

  1. HttpSession session1 =req .getSession();    
  2.  ServletContext Context = session1.getServletContext();    
  3.  // 这里面传递的是项目a的虚拟路径  
  4.  ServletContext Context1= Context.getContext("/myweb");  
  5.  System.out.println(Context1);  
  6.  HttpSession session2 =(HttpSession)Context1.getAttribute("session");  
  7.  System.out.println("base传过来的user为:"+session2.getAttribute("name"));  

  HttpSession session1 =req .getSession();   ServletContext Context = session1.getServletContext();   // 这里面传递的是项目a的虚拟路径  ServletContext Context1= Context.getContext("/myweb");  System.out.println(Context1);  HttpSession session2 =(HttpSession)Context1.getAttribute("session");  System.out.println("base传过来的user为:"+session2.getAttribute("name"));

然后重新部署就行了。       

 



原标题:tomcat session共享: 同tomcat不同项目之间的session共享

关键词:tomcat

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

【融资发布】MFS Africa完成超1亿美元C轮融资:https://www.kjdsnews.com/a/670555.html
亚马逊开放库存限制了?旺季补货不再发愁:https://www.kjdsnews.com/a/670556.html
Q4爆卖技巧!如何利用亚马逊捆绑销售抢流量?:https://www.kjdsnews.com/a/670557.html
80%亚马逊欧盟卖家都忽略了的欧盟B2B申报教程:https://www.kjdsnews.com/a/670558.html
干货!注意这几点,重大件、项目货出口运输就妥了!:https://www.kjdsnews.com/a/670559.html
亚马逊产品被下架的恢复方法:https://www.kjdsnews.com/a/670560.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流