[英]Session is expiring even when <session-timeout> is -1
在Web應用程序(Servlet-JSP MVC)中,我正在將會話超時設置為-1,這意味着會話將永遠不會過期,直到在注銷期間故意使會話無效為止。
<session-config>
<session-timeout>-1</session-timeout>
</session-config>
但是,如果用戶保持空閑狀態(即應用程序上沒有任何活動),然后在一段時間后刷新應用程序,則會話將過期。
我正在為XAMPP使用Apache Tomcat 7.0和我的應用程序。
可能是什么原因? 如何使會話無限期存活? 會話超時標記中的“ -1”實際上是什么意思?
更好的方法是使用ajax
調用刷新會話,但不要將會話超時設置得太長,因為用戶可以在不退出的情況下關閉瀏覽器,那么會話實體將保留在內存中,但永遠不會再使用。
您的設置不起作用可能是由於以下三個地方的設置沖突引起的:
(1)Java代碼session.setMaxInactiveInterval(600);
(2)webapp的web.xml
(3)Contianer的(tomcat?)設置conf/web.xml
或Catalina/localhost/yourapp/context.xml
或server.xml
或應用程序子模塊jar中的事件。
<Context path="/" docBase="/yourapp/base"
defaultSessionTimeOut="3600" ... />
優先級(1)>(2)>(3)
- - 編輯 - -
根據tomcat 7文檔,如果您使用SSL( https://tomcat.apache.org/tomcat-7.0-doc/config/http.html )
會話超時
創建SSL會話后將超時的時間(以秒為單位)。 使用0指定無限超時。 如果未指定,則使用>默認值86400(24小時)。
使用0指定無限超時
而此鏈接JSESSIONID Cookie和Tomcat中的到期日期,以及該https://stackoverflow.com/a/13463566/1484621值得一看
測試session
的正確方法是request.getSession(false) == null
或request.getSession(true).isNew()
。
根據源代碼
/**
* Set the default session timeout (in minutes) for this
* web application.
*
* @param timeout The new default session timeout
*/
@Override
public void setSessionTimeout(int timeout) {
int oldSessionTimeout = this.sessionTimeout;
/*
* SRV.13.4 ("Deployment Descriptor"):
* If the timeout is 0 or less, the container ensures the default
* behaviour of sessions is never to time out.
*/
this.sessionTimeout = (timeout == 0) ? -1 : timeout;
support.firePropertyChange("sessionTimeout",
oldSessionTimeout,
this.sessionTimeout);
}
session-timeout
設置為0或-1將具有相同的結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.