【问题标题】:How to read session-timout from web.xml如何从 web.xml 读取会话超时
【发布时间】:2013-07-08 13:49:28
【问题描述】:

如何从 web.xml 文件中读取session-timeout

试过了

getServletContext().getAttribute("session-timeout").toString(); 

但是给了我NullPointerException

也试过了

getServletConfig().getInitParameter("session-timeout");

但给我 null 因为显然这不是init-param

【问题讨论】:

    标签: servlets


    【解决方案1】:

    这是一个单线。

    int sessionTimeoutFromWebXml = Integer.parseInt(XPathFactory.newInstance().newXPath().compile("web-app/session-config/session-timeout").evaluate(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(getServletContext().getResourceAsStream("/WEB-INF/web.xml"))));
    

    ;)

    【讨论】:

    • 由于这段代码有点复杂,有没有办法从publicstatic变量中设置web.xml中的值?或者从ContextListenercontextInitialized 方法设置值?这样,我只将这个值写在一个地方。
    【解决方案2】:

    应该是

       session.getMaxInactiveInterval();
    

    它将以秒为单位返回时间。

    【讨论】:

    • 是的,我知道这一点,但是有没有办法从 web.xml 中按原样读取该属性?
    • 我什至在想,一个拥有如此良好声誉的人怎么可能不知道它;)。好吧,我想我们必须读取整个 xml 文件,找到 session-timeout 元素,然后检查值。我不太确定。也许这里的专家可以帮助我们俩
    • 实际上 getMaxInactiveInterval() 以秒为单位返回时间
    • 信息以分钟为单位存储,而您只能返回几秒钟,这有点奇怪。
    猜你喜欢
    • 2012-11-07
    • 2013-03-01
    • 2011-03-08
    • 2018-01-23
    • 1970-01-01
    • 2011-06-06
    • 2017-10-23
    • 1970-01-01
    • 2014-05-25
    相关资源
    最近更新 更多