1、在web容器中設置
例如:tomcat里面的web.xml
<session-config> <session-timeout>30</session-timeout> </session-config>
默認30分鍾,自己修改數字即可
2、在工程的web.xml中設置
例如:java web 項目中,在WebContent->WEB-INF->web.xml下
<session-config> <session-timeout>30</session-timeout> </session-config>
3、在Servlet中設置(Java代碼)
HttpSession session = request.getSession(); session.setMaxInactiveInterval(30*60);//以秒為單位
以上三種優先順序:1 < 2 <3
備注:
request.getSession()和request.getSession(true)意思相同:獲取session,如果session不存在,就新建一個
reqeust.getSession(false)獲取session,如果session不存在,則返回null
如果 項目中無法確定會話一定存在,最好用request.session(false);
例如:當向Session中存取登錄信息時,一般建議:HttpSession session =request.getSession();
當從Session中獲取登錄信息時,一般建議:HttpSession session =request.getSession(false);