java設定配置session到期時間的方法

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.