SessionTimeout:web.xml 与 session.maxInactiveInterval()

发布于 2024-09-07 05:04:56 字数 510 浏览 7 评论 0 原文

我正在尝试使 Java 中的 HttpSession 超时。我的容器是WebLogic。

目前,我们在 web.xml 文件中设置了会话超时,如下所示

<session-config>
    <session-timeout>15</session-timeout>
</session-config>

现在,我被告知这将在使用的第 15 分钟内终止会话(或者是所有会话?),无论其活动如何。

我想知道这种方法是否正确,或者我应该通过编程方式设置不活动的时间限制

session.setMaxInactiveInterval(15 * 60); //15 minutes

我不想在 15 分钟内删除所有会话,而只想删除那些在 15 分钟内处于非活动状态的会话。

这些方法等效吗?我应该选择 web.xml 配置吗?

I'm trying to timeout an HttpSession in Java. My container is WebLogic.

Currently, we have our session timeout set in the web.xml file, like this

<session-config>
    <session-timeout>15</session-timeout>
</session-config>

Now, I'm being told that this will terminate the session (or is it all sessions?) in the 15th minute of use, regardless their activity.

I'm wondering if this approach is the correct one, or should I programatically set the time limit of inactivity by

session.setMaxInactiveInterval(15 * 60); //15 minutes

I don't want to drop all sessions at 15 minutes, only those that have been inactive for 15 minutes.

Are these methods equivalent? Should I favour the web.xml config?

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

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

发布评论

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

评论(2

从﹋此江山别 2024-09-14 05:04:56

现在,我被告知这将在使用的第 15 分钟内终止会话(或者是所有会话?),无论他们的活动

这是错误。当关联的客户端(网络浏览器)超过 15 分钟没有访问该网站时,它只会终止会话。看到您尝试解决这个问题,该活动当然很重要,正如您最初预期的那样。

HttpSession#setMaxInactiveInterval顺便说一句, () 在这里并没有太大变化。它的作用与 web.xml 中的 完全相同,唯一的区别是您可以在运行时以编程方式更改/设置它。顺便说一句,更改仅影响当前会话实例,而不影响全局(否则它将是一个静态方法)。


要自己尝试和体验这一点,请尝试将 设置为 1 分钟,并创建一个 HttpSessionListener 如下所示:

@WebListener
public class HttpSessionChecker implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent event) {
        System.out.printf("Session ID %s created at %s%n", event.getSession().getId(), new Date());
    }

    public void sessionDestroyed(HttpSessionEvent event) {
        System.out.printf("Session ID %s destroyed at %s%n", event.getSession().getId(), new Date());
    }

}

(如果您不在 Servlet 上3.0 尚未使用,因此无法使用 @WebListener,然后在 web.xml 中注册,如下)

<listener>
    <listener-class>com.example.HttpSessionChecker</listener-class>
</listener>

请注意,servletcontainer 不会立即销毁会话正是超时值。它是一个后台作业,以一定的时间间隔运行(例如 5~15 分钟,具体取决于负载和 servlet 容器的 make/type)。因此,如果您在闲置一分钟后没有立即在控制台中看到 destroyed 行,请不要感到惊讶。但是,当您对超时但尚未销毁的会话发出 HTTP 请求时,它将立即被销毁。

另请参阅:

Now, i'm being told that this will terminate the session (or is it all sessions?) in the 15th minute of use, regardless their activity.

This is wrong. It will just kill the session when the associated client (webbrowser) has not accessed the website for more than 15 minutes. The activity certainly counts, exactly as you initially expected, seeing your attempt to solve this.

The HttpSession#setMaxInactiveInterval() doesn't change much here by the way. It does exactly the same as <session-timeout> in web.xml, with the only difference that you can change/set it programmatically during runtime. The change by the way only affects the current session instance, not globally (else it would have been a static method).


To play around and experience this yourself, try to set <session-timeout> to 1 minute and create a HttpSessionListener like follows:

@WebListener
public class HttpSessionChecker implements HttpSessionListener {

    public void sessionCreated(HttpSessionEvent event) {
        System.out.printf("Session ID %s created at %s%n", event.getSession().getId(), new Date());
    }

    public void sessionDestroyed(HttpSessionEvent event) {
        System.out.printf("Session ID %s destroyed at %s%n", event.getSession().getId(), new Date());
    }

}

(if you're not on Servlet 3.0 yet and thus can't use @WebListener, then register in web.xml as follows):

<listener>
    <listener-class>com.example.HttpSessionChecker</listener-class>
</listener>

Note that the servletcontainer won't immediately destroy sessions after exactly the timeout value. It's a background job which runs at certain intervals (e.g. 5~15 minutes depending on load and the servletcontainer make/type). So don't be surprised when you don't see destroyed line in the console immediately after exactly one minute of inactivity. However, when you fire a HTTP request on a timed-out-but-not-destroyed-yet session, it will be destroyed immediately.

See also:

旧故 2024-09-14 05:04:56

现在,我被告知这将在使用的第 15 分钟内终止会话(或者是所有会话?),无论其活动如何。

不,那不是真的。 session-timeout 配置不活动情况下的每个会话超时。

这些方法等效吗?我应该选择 web.xml 配置吗?

web.xml 中的设置是全局的,它适用于给定上下文的所有会话。您可以通过编程方式为特定会话更改此设置。

Now, i'm being told that this will terminate the session (or is it all sessions?) in the 15th minute of use, regardless their activity.

No, that's not true. The session-timeout configures a per session timeout in case of inactivity.

Are these methods equivalent? Should I favour the web.xml config?

The setting in the web.xml is global, it applies to all sessions of a given context. Programatically, you can change this for a particular session.

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