更好的方法是使用ajax
调用刷新会话,但不会将session-timeout设置太长,因为用户可以在不退出的情况下关闭浏览器,然后会话实体将保留在内存中但永远不会再次使用。
(1)的Java代码 session.setMaxInactiveInterval(600);
(2)Web应用的web.xml
(3)Contianer的(tomcat的:
您设置不起作用可以通过这样三个地方的设置冲突造成的? )设置conf/web.xml
或Catalina/localhost/yourapp/context.xml
或server.xml
或事件在您的应用程序的子模块罐子里。
<Context path="/" docBase="/yourapp/base"
defaultSessionTimeOut="3600" ... />
优先级(1)>(2)>(3)
---- ---- EDIT
根据Tomcat的7文档,如果使用SSL(https://tomcat.apache.org/tomcat-7.0-doc/config/http.html )
sessionTimeout
的时间,以秒为单位建立一个SSL会话后,它的Wi超时。使用0指定无限制超时。如果未指定,则使用缺省值86400(24小时)。
使用0指定无限超时
而这个链接JSESSIONID Cookie with Expiration Date in 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都会有同样的结果
我使用的第二个方法即设置会话超时在Web应用程序的web.xml中,就因为我知道它会覆盖容器会话超时值。关于AJAX调用方法,您能否对此有所了解?在哪些jsp中,我应该调用AJAX.i并没有明确的想法。 –
然后你必须检查你的代码在哪里使会话无效,是否有逻辑问题? –
我失效的会话唯一的地方是在LogOut控制器时,用户点击注销按钮会话将失效,除非我没有使其他地方的会话无效。为AJAX刷新提供一些输入。 –