简体   繁体   English

有没有办法通过一些调整在web.xml中以秒为单位设置会话超时?

[英]Is there any way to set session timeout in seconds in web.xml by doing some tweaking?

I have a requirement to set the session timeout of 40 seconds. 我要求将会话超时设置为40秒。 I know we keep normally to 20 minutes. 我知道我们通常保持20分钟。 But my current application requirement is to keep the session timeout in seconds that is to 40 seconds. 但是我当前的应用程序要求是将会话超时保持在40秒之内。 The web.xml is taking only integer value as 1 but it is not taking 0.6. web.xml仅将整数值作为1,但不采用0.6。 Is there any way to write this? 有什么办法写这个吗? We are running our java web application on Apache tomcat server. 我们在Apache tomcat服务器上运行Java Web应用程序。

The minimum value i am able to set in web.xml is 1 minute like: 我可以在web.xml中设置的最小值是1分钟,例如:

<session-config>
    <session-timeout>1</session-timeout>
</session-config>

I can set the session timeout to 40 seconds by using session.setMaxInactiveInterval(40); 我可以使用session.setMaxInactiveInterval(40)将会话超时设置为40秒; but the session.setMaxInactiveInterval(40); 但是session.setMaxInactiveInterval(40); works only when the website is opened by the user. 仅在用户打开网站时有效。 But as soon as the user closes the website session.setMaxInactiveInterval method will not work and the default web.xml takes control and again sets the session time out to 1 minute. 但是,一旦用户关闭网站session.setMaxInactiveInterval方法将不起作用,默认的web.xml将控制并再次将会话超时设置为1分钟。

Is there any way to set session timeout in seconds in web.xml by doing some tweaking? 有没有办法通过一些调整在web.xml中以秒为单位设置会话超时?

As far as I know the web.xml allows only for minutes. 据我所知, web.xml只允许几分钟。 If you want to use seconds, you'd have to do it programatically by registering a custom HttpSessionListener (or similar) in 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
    }
}


Taken from servlet-api-2.4 's HttpSession 取自servlet-api-2.4HttpSession

/**
 * 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM