同一Tomcat下不同Web应用之间共享Session会话

实现同一Tomcat下两个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.

设置为true 发现没有用,在网上搜了一下方法,基本是这样的:

由于每个WEB应用程序都有一个唯一的一个ServletContext 实例对象,自己下面的所有的servlet 共享此ServletContext,利用ServletContext 中的setAttribute() 方法把Session 传递过去,然后在另外一个WEB程序中拿到session实例。

1、修改Tomcat---conf----server.xml文件
把 
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" x  mlValidation="false"></Host> 
修改为:
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" x  mlValidation="false">
<Context path="/Project_A" reloadable="false" crossContext="true"></Context>
<Context path="/Project_B" reloadable="false" crossContext="true"></Context>
</Host>

注意 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、在 Project_A 中,写入以下代码:

//以下内容用于测试同一tomcat下不同项目之间共享session 
HttpSession session = req.getSession();
session.setAttribute("name", "testuser");
session.setMaxInactiveInterval(1800);
ServletContext ContextA =req.getSession().getServletContext();
ContextA.setAttribute("session", req.getSession());
//测试
out.println("IN SessionRangleServlet name : "+session.getAttribute("name"));

3、在 Project_B 中,写入以下代码取出Session

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

然后重新部署。


Tomcat下配置Session Cookie位置

最近部署一个Java应用的时候要求Session Cookie位置为根目录 “/” 而不是 /context。在配置Tomcat的时候碰到了一些问题,把我的解决过程写下来,希望给碰到同样问题的朋友一些帮助。

很多时候我们要求 Session Cookie的位置在根目录“/”这样多个应用可以互相交互。Tomcat的默认设置Session Cookie是在 /context 下。

在Tomcat 6 下,修改非常简单,只要在Connector 下增加 emptySessionPath="true" 属性就能解决了。可是到了Tomcat 7 ,这个配置不起作用了。

于是查了 Servlet 3.0 spec,发现Servlet 3.0 是允许 per-context basis 配置的,那么没有理由Tomcat 7 不支持啊。 

后来仔细研究了一下 Tomcat 7 的配置,原来Tomcat 7 把这个配置单独出来了,由一个sessionCookiePath属性了。

<Context ... sessionCookiePath="/" > ... </Context>

最后试验了一下,一切OK。
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值