2014-04-11 42 views
6

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

那麼如何在web.xml中設置會話超時秒數?

+0

訪問http://stackoverflow.com/questions/15382895/session-timeout-in-web-xml 瞭解更多信息 – harunaydin28

回答

11

使用部署描述符,你可以只設置超時分鐘:

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

但使用HttpSession的API,您可以設置以秒計的會話超時一個servlet:

HttpSession session = request.getSession(); 
session.setMaxInactiveInterval(40*60); 

建議閱讀:http://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/web_xml.html#1017275

+4

這將當作40分鐘不秒。 –

3

很好的web.xml文件,你可以在幾分鐘內提供

<session-config> 
    <session-timeout>Minutes</session-timeout> 
</session-config> 

但你編程提供值以秒

HttpSession session = request.getSession(); 
session.setMaxInactiveInterval(20*60); 
0
1) Timeout in the deployment descriptor (web.xml) 

- 指定爲「分」的超時值,以「會話配置」元素包圍。

標記

<web-app ...> 
    <session-config> 
     <session-timeout>20</session-timeout> 
    </session-config> 
</web-app> 

上面設置適用於整個web應用,以及會議將由容器被殺死,如果客戶端沒有在20分鐘後進行任何請求。

2) Timeout with setMaxInactiveInterval() 

- 您可以手動爲特定會話在「秒」中指定超時值。

爪哇

HttpSession session = request.getSession(); 
session.setMaxInactiveInterval(20*60); 

上述設置僅應用在其上調用「的setMaxInactiveInterval()」方法會話,並且會話將因容器的被殺死,如果客戶端沒有在20分鐘後進行任何請求。

0

您可以通過「setMaxInactiveInterval()」覆蓋會話超時。

HttpSession session = request.getSession(); 
session.setMaxInactiveInterval(20000); 

這裏它將花費時間以毫秒爲單位,意味着在接下來的20秒內會話將會過期。

+1

setMaxInactiveInterval以秒爲單位獲取值 –