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

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

问题描述

我一直在寻找在多个warl文件之间共享会话数据的解决方案。我遇到了以下解决方案: 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文件,你可以使用session的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 applicatons

/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上你可以将 emptySessionPath 属性设置为 true /conf/server.xml < Connector> 元素中>。

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配置。然后可以通过在< Context>中设置 sessionCookiePath / 来配置Tomcat一侧 context.xml 文件中的code>元素。

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()

You could do this in ServletContextListener#contextInitialized() or 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天全站免登陆