本文實(shí)例講述了java設(shè)置session過(guò)期時(shí)間的實(shí)現(xiàn)方法,分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
1、Timeout in the deployment descriptor (web.xml)
以分鐘為單位
<session-config>
<session-timeout>20</session-timeout>
</session-config>
</web-app>
上面這種設(shè)置,對(duì)整個(gè)web應(yīng)用生效。當(dāng)客戶端20分鐘內(nèi)都沒(méi)有發(fā)起請(qǐng)求時(shí),容器會(huì)將session干掉。
2、Timeout with setMaxInactiveInterval()
通過(guò)編碼方式,指定特定的session的過(guò)期時(shí)間,以秒為單位。例如:
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、在程序中定義,單位為秒,設(shè)置為-1表示永不過(guò)期,示例代碼為:
Session設(shè)置產(chǎn)生效果的優(yōu)先循序是,先程序后配置,先局部后整體。
希望本文所述對(duì)大家的Java程序設(shè)計(jì)有所幫助。