How to read session timeout from web.xml

How can I read session-timeout from web.xml?

I tried

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

but gives me a NullPointerException .

Also tried

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

But gives me null, because obviously this is not init-param

+4
source share
2 answers

Here is oneliner.

 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")))); 

;)

+7
source

It should be

  session.getMaxInactiveInterval(); 

It will return the time in seconds.

+1
source

Source: https://habr.com/ru/post/1490293/


All Articles