如何在 Tomcat 中保持 HttpSession 活动?

发布于 2024-09-16 08:07:16 字数 408 浏览 2 评论 0原文

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

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

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

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

阿帕奇汤姆猫/6.0.20

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>

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑忘罢 2024-09-23 08:07:16

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

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

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

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

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

“通过在其中包含会话 ID 来对指定的 URL 进行编码,或者,如果不需要编码,则返回原样的 URL。此方法的实现包括确定会话 ID 是否需要的逻辑例如,如果浏览器支持 cookie,或者关闭会话跟踪,则不需要 URL 编码。

为了实现稳健的会话跟踪,servlet 发出的所有 URL 都应通过此运行 。否则,URL重写不能用于不支持cookie的浏览器。”

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

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.

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

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.

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:

"Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. The implementation of this method includes the logic to determine whether the session ID needs to be encoded in the URL. For example, if the browser supports cookies, or session tracking is turned off, URL encoding is unnecessary.

For robust session tracking, all URLs emitted by a servlet should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文