Java SessionTimeout:web.xml 与 session.maxInactiveInterval()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3118968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SessionTimeout: web.xml vs session.maxInactiveInterval()
提问by Tom
I'm trying to timeout an HttpSessionin Java. My container is WebLogic.
我正在尝试在 Java中超时 HttpSession。我的容器是 WebLogic。
Currently, we have our session timeout set in the web.xmlfile, like this
目前,我们在web.xml文件中设置了会话超时,如下所示
<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.
现在,我被告知这将在使用的第 15 分钟终止会话(还是所有会话?),无论他们的活动如何。
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.
我不想在 15 分钟时放弃所有会话,只放弃那些 15 分钟不活动的会话。
Are these methods equivalent? Should I favour the web.xmlconfig?
这些方法等价吗?我应该喜欢web.xml配置吗?
采纳答案by BalusC
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.
现在,我被告知这将在使用的第 15 分钟终止会话(或者是所有会话?),不管他们的活动。
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.
这是错误的。当关联的客户端(网络浏览器)超过 15 分钟未访问该网站时,它只会终止会话。活动当然很重要,正如您最初预期的那样,看到您尝试解决此问题。
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).
在HttpSession#setMaxInactiveInterval()
不通过的方式在这里多变化。它不完全一样<session-timeout>
的web.xml
,唯一的区别,你可以改变/运行时编程设定。顺便说一下,更改只影响当前会话实例,而不是全局的(否则它会是一个static
方法)。
To play around and experience this yourself, try to set <session-timeout>
to 1 minute and create a HttpSessionListener
like follows:
要自己尝试并体验这一点,请尝试将时间设置<session-timeout>
为 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());
}
}
(if you're not on Servlet 3.0 yet and thus can't use @WebListener
, then register in web.xml
as follows):
(如果您尚未使用 Servlet 3.0,因此无法使用@WebListener
,请web.xml
按如下方式注册):
<listener>
<listener-class>com.example.HttpSessionChecker</listener-class>
</listener>
Note that the servletcontainer won't immediately destroy sessions after exactlythe 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.
请注意,servletcontainer 不会在恰好超时值之后立即销毁会话。这是一个以特定时间间隔运行的后台作业(例如 5~15 分钟,取决于负载和 servletcontainer make/type)。因此,当您destroyed
在正好一分钟不活动后没有立即在控制台中看到一行时,请不要感到惊讶。但是,当您在超时但尚未销毁的会话上触发 HTTP 请求时,它将立即销毁。
See also:
也可以看看:
回答by Pascal Thivent
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.
现在,我被告知这将在使用的第 15 分钟终止会话(还是所有会话?),无论他们的活动如何。
No, that's not true. The session-timeout
configures a per session timeout in case of inactivity.
不,这不是真的。该session-timeout
配置在每个会话超时在闲置情况下。
Are these methods equivalent? Should I favour the web.xml config?
这些方法等价吗?我应该喜欢 web.xml 配置吗?
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.
web.xml 中的设置是全局的,它适用于给定上下文的所有会话。以编程方式,您可以为特定会话更改此设置。