Java web 中Session有效時間設置


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);


免責聲明!

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



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