【问题标题】:How to reset the timeout counter of a HTTPSession?如何重置 HTTPSession 的超时计数器?
【发布时间】:2012-11-05 22:06:24
【问题描述】:

除了提供 HTTP 请求之外,还有其他方法可以重置 HTTPSession 的超时计数器吗?

我正在寻找类似的东西

session.resetTimeout();

【问题讨论】:

    标签: java servlets httpsession


    【解决方案1】:

    那么,你手头有一个HttpSession,却没有一个具体的HttpServletRequest

    可以max inactive intervalcurrent inactive timemax inactive interval 的总和相加。

    int maxInactiveInterval = session.getMaxInactiveInterval();
    int currentInactiveTime = (int) (System.currentTimeMillis() - session.getLastAccessedTime() / 1000);
    session.setMaxInactiveInterval(maxInactiveInterval + currentInactiveTime);
    

    但是,这需要一些filter,当它的值偏离默认值时,它会在每次请求时再次将其重置回默认的最大非活动间隔。

    session.setMaxInactiveInterval(defaultMaxInactiveInterval);
    

    在 Servlet 3.0 上,此值可通过 SessionCookieConfig#getMaxAge() 获得。

    int defaultMaxInactiveInterval = getServletContext().getSessionCookieConfig().getMaxAge();
    

    【讨论】:

      猜你喜欢
      • 2013-03-12
      • 2010-11-20
      • 1970-01-01
      • 2021-03-11
      • 2011-12-28
      • 1970-01-01
      • 2013-08-22
      • 2020-05-01
      • 1970-01-01
      相关资源
      最近更新 更多