Java 如何在 web.xml 中以秒为单位设置会话超时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23005600/
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
How do i set session timeout in seconds in web.xml?
提问by user3488632
I have a requirement to set the session timeout of 40 seconds. I know we keep normally to 20 minutes. But my current application requirement is to keep the session timeout to 40 seconds. The web.xml is taking only integer value as 1 but it is not taking 0.6. Is there any way to write this? We are running our java web application on Apache tomcat server.
我需要将会话超时设置为 40 秒。我知道我们通常保持 20 分钟。但是我当前的应用程序要求是将会话超时保持在 40 秒。web.xml 仅将整数值设为 1,但并未将其设为 0.6。有没有办法写这个?我们在 Apache tomcat 服务器上运行我们的 java web 应用程序。
So how do i set session timeout in seconds in web.xml?
那么如何在 web.xml 中以秒为单位设置会话超时?
回答by Juned Ahsan
Using the deployment descriptor, you can only set the timeout in minutes:
使用部署描述符,您只能以分钟为单位设置超时:
<session-config>
<session-timeout>1</session-timeout>
</session-config>
But using the HttpSessionAPI you can set the session timeout in secondsfor a servlet container:
但是使用HttpSessionAPI,您可以为servlet 容器设置会话超时(以秒为单位):
HttpSession session = request.getSession();
session.setMaxInactiveInterval(40);
Suggested reading: Deployment Descriptor Elements
建议阅读:部署描述符元素
回答by Sanjay Rabari
well in web.xml file you can provide in minutes
在 web.xml 文件中,您可以在几分钟内提供
<session-config>
<session-timeout>Minutes</session-timeout>
</session-config>
but you programatically provide values in seconds
但是您以编程方式在几秒钟内提供值
HttpSession session = request.getSession();
session.setMaxInactiveInterval(20*60);
回答by Mohit Singh
1) Timeout in the deployment descriptor (web.xml)
– Specified the timeout value in “minute” , enclose with “session-config” element.
– 在“minute”中指定超时值,用“session-config”元素括起来。
Markup
标记
<web-app ...>
<session-config>
<session-timeout>20</session-timeout>
</session-config>
</web-app>
The above setting is apply for the entire web application, and session will be kill by container if client doesn't make any request after 20 minutes.
以上设置适用于整个Web应用程序,如果客户端在20分钟后没有发出任何请求,会话将被容器杀死。
2) Timeout with setMaxInactiveInterval()
– You can manually specified the timeout value in “second” for a particular session.
– 您可以为特定会话手动指定以“秒”为单位的超时值。
Java
爪哇
HttpSession session = request.getSession();
session.setMaxInactiveInterval(20*60);
The above setting is only apply on session which call the “setMaxInactiveInterval()” method, and session will be kill by container if client doesn't make any request after 20 minutes.
上述设置仅适用于调用“setMaxInactiveInterval()”方法的会话,如果客户端在 20 分钟后没有发出任何请求,会话将被容器杀死。
回答by Rahul Kumar
you can override the session timeout through "setMaxInactiveInterval()".
您可以通过“setMaxInactiveInterval()”覆盖会话超时。
HttpSession session = request.getSession();
session.setMaxInactiveInterval(20000);
here it will take the time in milliseconds, means in next 20 seconds session will get expire.
这里需要以毫秒为单位的时间,这意味着在接下来的 20 秒内会话将过期。