java web 超时设置,如何在Java Web应用程序中动态设置会话超时?

I need to give my user a web interface to change the session timeout interval. So, different installations of the web application would be able to have different timeouts for their sessions, but their web.xml cannot be different.

Is there a way to set the session timeout programatically, so that I could use, say, ServletContextListener.contextInitialized() to read the configured interval and set it upon application startup?

Thanks a lot.

解决方案

Instead of using a ServletContextListener, use a HttpSessionListener. In the sessionCreated() method, you can set the session timeout programmatically.

public class MyHttpSessionListener implements HttpSessionListener{

public void sessionCreated(HttpSessionEvent event){

event.getSession().setMaxInactiveInterval(15*60); //in seconds

}

public void sessionDestroyed(HttpSessionEvent event){}

}

And don't forget to define the listener in the deployment descriptor:

...

com.example.MyHttpSessionListener

Still, I would recommend creating different web.xml files for each application and defining the session timeout there:

...

15

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值