java设置session过期时间

来源:爱站网时间:2020-05-11编辑:网友分享
今天本文将为大家介绍java设置session过期时间的方法,相信这个问题是很多小伙伴们都是非常想了解的,那么下面我们就一起去看看具体的内容吧。

今天本文将为大家介绍java设置session过期时间的方法,相信这个问题是很多小伙伴们都是非常想了解的,那么下面我们就一起去看看具体的内容吧。

1、Timeout in the deployment descriptor (web.xml)
以分钟为单位

 

 


20


上面这种设置,对整个web应用生效。当客户端20分钟内都没有发起请求时,容器会将session干掉。

 

2、Timeout with setMaxInactiveInterval()
通过编码方式,指定特定的session的过期时间,以秒为单位。例如:

 

 
HttpSession session = request.getSession();
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、在程序中定义,单位为秒,设置为-1表示永不过期,示例代码为:

 

 
session.setMaxInactiveInterval(30*60);


Session设置产生效果的优先循序是,先程序后配置,先局部后整体。

 

上文就是java设置session过期时间的方法,需要的朋友赶紧动手尝试一下吧,这在我们日后使用计算机的过程中会带来不少方便。

上一篇:java满天星效果的实现方法

下一篇:java怎么实现mp3合并

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载