如何在 Tomcat 中保持 HttpSessions 存活? [英] How Do I Keep HttpSessions Alive in Tomcat?

查看:37
本文介绍了如何在 Tomcat 中保持 HttpSessions 存活?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Tomcat 服务的 Web 应用程序中遇到了会话超时问题.通过阅读 Tomcat 的文档,会话会在一段时间后过期,该时间可以在 web.xml 文件中配置.

I'm having a bit of trouble with Session timeouts in my Tomcat served web application. From reading over Tomcat's documentation, sessions expire after a time which can be configured in the web.xml file.

<session-config>
        <session-timeout>30</session-timeout>
    </session-config>

问题)这是否意味着从为用户创建会话起 30 分钟?还是距离上次访问会话 30 分钟?

Quesion) Does this mean 30 minutes from when the session was created for the user? Or 30 minutes from when the session was last accessed?

如果是,正如我最初认为的那样,距离上次访问会话 30 分钟,我似乎没有看到这种行为.当我使用该网站时,我的会话似乎丢失了.除了这一设置之外,还有其他方法可以配置会话行为吗?有什么我遗漏的吗?

If it is, as I originally thought, 30 minutes from when the session was last accessed, I don't seem to be seeing this behavior. My sessions seem to be lost as I'm using the site. Are there any other ways to configure session behavior besides this one setting? Is there something I'm missing?

Apache Tomcat/6.0.20

Apache Tomcat/6.0.20

推荐答案

当 Web 浏览器连接到您的应用程序时,它会启动一个会话.当最长不活动时间(30 分钟)过后,Tomcat 会关闭服务器上的会话.

A session is started for the web browser when it connects to your application. Tomcat closes the session on the server when the maximum period of inactivity has passed (30 minutes).

只要 Web 浏览器上有活动,例如刷新当前页面或浏览应用程序控制下的其他页面,就会重置此超时.仅仅保持浏览器窗口打开并不能保持会话打开,因为它不会在浏览器上生成任何活动.

This timeout is reset whenever there is activity on the web browser, such as refreshing the current page or navigating through other pages under the application control. Merely keeping a browser window open does not keep the session open because it does not generate any activity on the browser.

您可以按照您的描述在 web.xml 文件中进行设置.

You can set it in the web.xml file as you described.

您也可以通过调用 setMaxInactiveInterval(int interval)session 对象设置它这指定了 servlet 容器使此会话无效之前客户端请求之间的时间(以秒为单位).

You can also set it for the session object by calling setMaxInactiveInterval(int interval) This specifies the time, in seconds, between client requests before the servlet container will invalidate this session.

您必须确保为您的浏览器启用cookies.否则,您为每个请求创建一个新会话.您应该为应用程序中的每个 URL 调用 HttpServletResponse.encodeURL(String url).来自 api 文档:

You have to make sure, that the cookies are enabled for your browser. Otherwise you create a new Session with each request. You should call the HttpServletResponse.encodeURL(String url) for each URL in your application. From the api doc:

"通过在其中包含会话ID对指定的URL进行编码,或者,如果不需要编码,则返回URL不变.该方法的实现包括判断会话ID是否需要编码的逻辑在 URL 中.例如,如果浏览器支持 cookie,或者关闭了会话跟踪,则不需要 URL 编码.

为了健壮的会话跟踪,servlet 发出的所有 URL 都应该通过这个方法运行.否则,不支持 cookie 的浏览器将无法使用 URL 重写."

这篇关于如何在 Tomcat 中保持 HttpSessions 存活?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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