在 Tomcat 中的上下文之间共享会话数据 [英] Sharing session data between contexts in Tomcat

查看:27
本文介绍了在 Tomcat 中的上下文之间共享会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找在多个战争文件之间共享会话数据的解决方案.我遇到了以下解决方案 http://www.fwd.at/tomcat/sharing-session-data-howto.html

I have been looking at solutions for sharing session data between mutliple war files. I came across the following solution http://www.fwd.at/tomcat/sharing-session-data-howto.html

它的基本思想是,如果你有多个war文件,你可以使用第一个上下文的sessionid设置一个cookie.

The basic idea of it is that if you have more than one war file, you can set a cookie using the sessionid of the first context that is used.

可以使用适用于所有上下文/应用程序的路径设置 cookie.

The cookie can be set using a path that will apply to all contexts/applications.

例如,如果我有以下 3 个应用程序的配置

For example, if I have the following configuration for 3 applications

/myapp/app1
/myapp/app2
/myapp/app3

我可以如下设置cookie

I can set a cookie as follows

/myapp sessionid.

/myapp sessionid.

然后 sessionid cookie 将被发送到地址中包含/myapp 的任何请求.这允许会话 id 被任何上下文使用.

The sessionid cookie will then be sent to any request with /myapp in the address. This allows the session id to then be used by any of the contexts.

这种方法的唯一问题是它写于 2003 年,并在 Tomcat 4 上进行了测试.

The only problem with this approach is that it was written in 2003 and tested on Tomcat 4.

您对这种方法有何看法?有没有更好的方法?

What are your opinions of this approach? Is there a better way of doing it?

谢谢

推荐答案

那篇文章确实已经过时了.

That article is indeed heavily outdated.

在 Tomcat 5.5 和 6.0 上,您可以在 /conf 中的 元素中将 emptySessionPath 属性设置为 true/server.xml.

On Tomcat 5.5 and 6.0 you can just set emptySessionPath attribute to true in the <Connector> element in /conf/server.xml.

<Connector ... emptySessionPath="true">

在 Tomcat 7.0 上,这已经改变,因为现在可以从 Servlet 3.0 API 开始配置.然后在 Tomcat 端,通过在任何负责的 context.xml 元素中将 sessionCookiePath 设置为 / 来配置它> 文件.

On Tomcat 7.0 this has changed because this is now configureable from the Servlet 3.0 API on. It's then on Tomcat's side configureable by setting sessionCookiePath to / in <Context> element in any responsible context.xml file.

<Context ... sessionCookiePath="/">

如前所述,有一个新的 Servlet 3.0 API,它允许您通过标准 API 配置会话 cookie.您可以通过将以下内容添加到 web.xml 中以声明方式执行此操作:

As said, there's a new Servlet 3.0 API which allows you to configure the session cookie through the standard API. You can do it either declaratively by adding the following to the web.xml:

<session-config>
    <cookie-config>
        <path>/</path>
    </cookie-config>
</session-config>

或以编程方式通过 SessionCookieConfig 可通过 ServletContext#getSessionCookieConfig().

or programmatically by SessionCookieConfig which is available by ServletContext#getSessionCookieConfig().

getServletContext().getSessionCookieConfig().setPath("/");

您可以在 ServletContextListener#contextInitialized()HttpServlet#init().

  • Tomcat 5.5 HTTP connector documentation
  • Tomcat 6.0 HTTP connector documentation - mentions potential security hole
  • Tomcat 7.0 context documentation

这篇关于在 Tomcat 中的上下文之间共享会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆