[英]Is there any way to set session timeout in seconds in web.xml by doing some tweaking?
我要求将会话超时设置为40秒。 我知道我们通常保持20分钟。 但是我当前的应用程序要求是将会话超时保持在40秒之内。 web.xml仅将整数值作为1,但不采用0.6。 有什么办法写这个吗? 我们在Apache tomcat服务器上运行Java Web应用程序。
我可以在web.xml中设置的最小值是1分钟,例如:
<session-config>
<session-timeout>1</session-timeout>
</session-config>
我可以使用session.setMaxInactiveInterval(40)将会话超时设置为40秒; 但是session.setMaxInactiveInterval(40); 仅在用户打开网站时有效。 但是,一旦用户关闭网站session.setMaxInactiveInterval方法将不起作用,默认的web.xml将控制并再次将会话超时设置为1分钟。
有没有办法通过一些调整在web.xml中以秒为单位设置会话超时?
据我所知, web.xml
只允许几分钟。 如果要使用秒,则必须通过在web.xml
注册一个自定义HttpSessionListener
(或类似名称)来以编程方式进行操作:
<listener>
<listener-class>com.sample.SessionTimeoutSetter</listener-class>
</listener>
public class SessionTimeoutSetter implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
event.getSession().setMaxInactiveInterval(40);
}
public void sessionDestroyed(HttpSessionEvent event) {
// not needed
}
}
取自servlet-api-2.4
的HttpSession
/**
* Specifies the time, in seconds, between client requests before the
* servlet container will invalidate this session. A negative time
* indicates the session should never timeout.
*
* @param interval An integer specifying the number of seconds
*/
public void setMaxInactiveInterval(int interval);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.