如何利用cookie实现不同服务器间的session共享

2025-04-30 12:07:49
推荐回答(1个)
回答1:

实现同一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文件


修改为:




注意 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.