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

发布于 2024-09-04 17:41:04 字数 197 浏览 2 评论 0原文

我需要为我的用户提供一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

会傲 2024-09-11 17:41:04

不要使用 ServletContextListener,而是使用 HttpSessionListener< /代码>

sessionCreated() 方法,您可以通过编程方式设置会话超时

public class MyHttpSessionListener implements HttpSessionListener {

  public void sessionCreated(HttpSessionEvent event){
      event.getSession().setMaxInactiveInterval(15 * 60); // in seconds
  }

  public void sessionDestroyed(HttpSessionEvent event) {}

}

并且不要忘记在中定义监听器 em>部署描述符:(

<webapp>
...      
  <listener>                                  
    <listener-class>com.example.MyHttpSessionListener</listener-class>
  </listener>
</webapp>

或者从 Servlet 版本 3.0 开始,您可以使用 @WebListener 注释代替)。

不过,我建议为每个应用程序创建不同的 web.xml 文件并在其中定义会话超时:

<webapp>
...
  <session-config>
    <session-timeout>15</session-timeout> <!-- in minutes -->
  </session-config>
</webapp>

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:

<webapp>
...      
  <listener>                                  
    <listener-class>com.example.MyHttpSessionListener</listener-class>
  </listener>
</webapp>

(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:

<webapp>
...
  <session-config>
    <session-timeout>15</session-timeout> <!-- in minutes -->
  </session-config>
</webapp>
手长情犹 2024-09-11 17:41:04

有没有办法以编程方式设置会话超时

设置会话超时值基本上有三种方法:

但请注意,后面的选项设置当前会话的超时值,这不是全局设置。

Is there a way to set the session timeout programatically

There are basically three ways to set the session timeout value:

  • by using the session-timeout in the standard web.xml file ~or~
  • in the absence of this element, by getting the server's default session-timeout value (and thus configuring it at the server level) ~or~
  • programmatically by using the 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.

携君以终年 2024-09-11 17:41:04

正如另一个答案所说,您可以在会话侦听器中进行更改。但例如,您可以直接在 servlet 中更改它。

getRequest().getSession().setMaxInactiveInterval(123);

As another anwsers told, you can change in a Session Listener. But you can change it directly in your servlet, for example.

getRequest().getSession().setMaxInactiveInterval(123);
他是夢罘是命 2024-09-11 17:41:04

我需要为我的用户提供一个 Web 界面来更改会话超时间隔。因此,不同安装的 Web 应用程序的会话超时时间可能不同,但它们的 web.xml 不能不同。

你的问题很简单,你需要会话超时间隔应该在运行时配置,并且配置应该通过Web界面完成,并且不应该有重新启动服务器的开销。

我扩展迈克尔的回答来解决你的问题。

逻辑:您需要将配置的值存储在.properties 文件或数据库中。在服务器启动时读取存储的值并复制到变量,使用该变量直到服务器启动。随着配置的更新,变量也会更新。
就是这样。

说明

MyHttpSessionListener 类中的
1. 创建一个名为 globalSessionTimeoutInterval 的静态变量。

  1. 创建一个静态块(仅在第一次访问类时执行)并从 config.properties 文件中读取超时值并将值设置为 globalSessionTimeoutInterval 变量。

  2. 现在使用该值设置 maxInactiveInterval

  3. 现在 Web 部件,即管理配置页面

    a.将配置的值复制到静态变量globalSessionTimeoutInterval。

    b.将相同的值写入 config.properties 文件。
    (考虑服务器重新启动,然后 globalSessionTimeoutInterval 将加载 config.properties 文件中存在的值)

  4. 替代 .properties 文件或将其存储到数据库中。选择权在您。

用于实现相同的逻辑代码

public class MyHttpSessionListener implements HttpSessionListener 
{
  public static Integer globalSessionTimeoutInterval = null;

  static
  {
      globalSessionTimeoutInterval =  Read value from .properties file or database;
  }
  public void sessionCreated(HttpSessionEvent event)
  {
      event.getSession().setMaxInactiveInterval(globalSessionTimeoutInterval);
  }

  public void sessionDestroyed(HttpSessionEvent event) {}

}

并且在您的配置控制器或配置 servlet 中

String valueReceived = request.getParameter(timeoutValue);
if(valueReceived  != null)
{
    MyHttpSessionListener.globalSessionTimeoutInterval = Integer.parseInt(timeoutValue);
          //Store valueReceived to config.properties file or database
}

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.

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.

  1. 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.

  2. Now use that value to set maxInactiveInterval

  3. 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)

  4. Alternative .properties file OR storing it into database. Choice is yours.

Logical code for achieving the same

public class MyHttpSessionListener implements HttpSessionListener 
{
  public static Integer globalSessionTimeoutInterval = null;

  static
  {
      globalSessionTimeoutInterval =  Read value from .properties file or database;
  }
  public void sessionCreated(HttpSessionEvent event)
  {
      event.getSession().setMaxInactiveInterval(globalSessionTimeoutInterval);
  }

  public void sessionDestroyed(HttpSessionEvent event) {}

}

And in your Configuration Controller or Configuration servlet

String valueReceived = request.getParameter(timeoutValue);
if(valueReceived  != null)
{
    MyHttpSessionListener.globalSessionTimeoutInterval = Integer.parseInt(timeoutValue);
          //Store valueReceived to config.properties file or database
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文