java設置配置session過期時間的方法


1) Timeout in the deployment descriptor (web.xml)
以分鍾為單位

代碼如下 復制代碼
<web-app ...>
<session-config>
<session-timeout>20</session-timeout>
</session-config>
</web-app>

上面這種設置,對整個web應用生效。當客戶端20分鍾內都沒有發起請求時,容器會將session干掉。

2) Timeout with setMaxInactiveInterval()
通過編碼方式,指定特定的session的過期時間,以秒為單位。例如:

代碼如下 復制代碼
HttpSession session = request.getSession();
session.setMaxInactiveInterval(20*60);

The above setting is only apply on session which call the “setM(www.111cn.net)axInactiveInterval()” method, and session will be kill by container if client doesn’t make any request after 20 minutes.

Thoughts….
This is a bit confusing , the value in deployment descriptor (web.xml) is in “minute”, but the setMaxInactiveInterval() method is accept the value in “second”. Both functions should synchronize it in future release

3) 在程序中定義,單位為秒,設置為-1表示永不過期,示例代碼為:

代碼如下 復制代碼
session.setMaxInactiveInterval(30*60);

Session設置產生效果的優先循序是,先程序后配置,先局部后整體
from:http://www.111cn.net/jsp/Java/59186.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM