如何在Java Web应用程序中动态设置会话超时?
我需要为我的用户提供一个 Web 界面来更改会话超时间隔。因此,不同安装的 Web 应用程序的会话超时时间可能不同,但它们的 web.xml
不能不同。
有没有办法以编程方式设置会话超时,以便我可以使用 ServletContextListener.contextInitialized() 来读取配置的时间间隔并在应用程序启动时设置它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用 ServletContextListener,而是使用
HttpSessionListener< /代码>
。
在
sessionCreated()
方法,您可以通过编程方式设置会话超时:并且不要忘记在中定义监听器 em>部署描述符:(
或者从 Servlet 版本 3.0 开始,您可以使用
@WebListener
注释代替)。不过,我建议为每个应用程序创建不同的 web.xml 文件并在其中定义会话超时:
Instead of using a ServletContextListener, use a
HttpSessionListener
.In the
sessionCreated()
method, you can set the session timeout programmatically:And don't forget to define the listener in the deployment descriptor:
(or since Servlet version 3.0 you can use
@WebListener
annotation instead).Still, I would recommend creating different web.xml files for each application and defining the session timeout there:
设置会话超时值基本上有三种方法:
web.xml
文件中的session-timeout
~或者~session-timeout
值(从而在服务器级别配置它)~或者~HttpSession。 Servlet 或 JSP 中的 setMaxInactiveInterval(int secondary)
方法。但请注意,后面的选项设置当前会话的超时值,这不是全局设置。
There are basically three ways to set the session timeout value:
session-timeout
in the standardweb.xml
file ~or~session-timeout
value (and thus configuring it at the server level) ~or~HttpSession. setMaxInactiveInterval(int seconds)
method in your Servlet or JSP.But note that the later option sets the timeout value for the current session, this is not a global setting.
正如另一个答案所说,您可以在会话侦听器中进行更改。但例如,您可以直接在 servlet 中更改它。
As another anwsers told, you can change in a Session Listener. But you can change it directly in your servlet, for example.
你的问题很简单,你需要会话超时间隔应该在运行时配置,并且配置应该通过Web界面完成,并且不应该有重新启动服务器的开销。
我扩展迈克尔的回答来解决你的问题。
逻辑:您需要将配置的值存储在.properties 文件或数据库中。在服务器启动时读取存储的值并复制到变量,使用该变量直到服务器启动。随着配置的更新,变量也会更新。
就是这样。
说明
MyHttpSessionListener 类中的
1. 创建一个名为 globalSessionTimeoutInterval 的静态变量。
创建一个静态块(仅在第一次访问类时执行)并从 config.properties 文件中读取超时值并将值设置为 globalSessionTimeoutInterval 变量。
现在使用该值设置 maxInactiveInterval
现在 Web 部件,即管理配置页面
a.将配置的值复制到静态变量globalSessionTimeoutInterval。
b.将相同的值写入 config.properties 文件。
(考虑服务器重新启动,然后 globalSessionTimeoutInterval 将加载 config.properties 文件中存在的值)
替代 .properties 文件或将其存储到数据库中。选择权在您。
用于实现相同的逻辑代码
并且在您的配置控制器或配置 servlet 中
your question is simple, you need session timeout interval should be configurable at run time and configuration should be done through web interface and there shouldn't be overhead of restarting the server.
I am extending Michaels answer to address your question.
Logic: You need to store configured value in either .properties file or to database. On server start read that stored value and copy to a variable use that variable until server is UP. As config is updated update variable also.
Thats it.
Expaination
In MyHttpSessionListener class
1. create a static variable with name globalSessionTimeoutInterval.
create a static block(executed for only for first time of class is being accessed) and read timeout value from config.properties file and set value to globalSessionTimeoutInterval variable.
Now use that value to set maxInactiveInterval
Now Web part i.e, Admin configuration page
a. Copy configured value to static variable globalSessionTimeoutInterval.
b. Write same value to config.properties file.
(consider server is restarted then globalSessionTimeoutInterval will be loaded with value present in config.properties file)
Alternative .properties file OR storing it into database. Choice is yours.
Logical code for achieving the same
And in your Configuration Controller or Configuration servlet