2014-04-11 13 views
0

我有一個要求將會話超時設置爲40秒。我知道我們通常保持20分鐘。但我目前的應用程序要求是保持會話超時秒數達到40秒。 web.xml只取整數值1,但不取0.6。有什麼方法可以寫這個嗎?我們正在Apache Tomcat服務器上運行我們的Java Web應用程序。有什麼方法可以通過做一些調整來在web.xml中設置會話超時秒數?

最小值我能夠在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中設置會話超時秒數?

回答

1

據我所知,web.xml只允許幾分鐘。如果你想使用秒,你必須通過註冊一個自定義HttpSessionListener(或類似)在web.xml做編程:

<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.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); 
+0

然後,我必須寫什麼來代替1 1 在web.xml文件中 – user3488632

+0

您可以對它進行註釋,指出它是由您的類設置的 – Morfic

相關問題