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 “setMaxInactiveInterval()” 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設定產生效果的優先循序是,先程式後配置,先局部後整體。