14

I'm trying to set the session-timeout value in an embedded Jetty 8 instance.

With embedded Jetty, how can I programmatically set the session-timeout value that would otherwise be set in the web.xml as follows:

 <session-config>
     <session-timeout>15</session-timeout>
 </session-config>

Thanks!

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
HolySamosa
  • 9,011
  • 14
  • 69
  • 102

2 Answers2

15

Access the session handling / management on your WebAppContext and set it.

WebAppContext app = new WebAppContext(....);
... 
app.getSessionHandler().getSessionManager().setMaxInactiveInterval(timeout);

This is how Jetty itself does it.

Note: SessionManager.setMaxInactiveInterval(int) is in seconds, not milliseconds.

Voicu
  • 16,921
  • 10
  • 60
  • 69
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
4

2019-05-11

For Jetty version 9.4.12.v20180830 with the following ServletContextHandler setup, this is:

ServletContextHandler webappContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
... 
webappContext.getSessionHandler().setMaxInactiveInterval(timeout_in_sec);

(There is no intermediate call to getSessionManager())

Oliver
  • 1,218
  • 1
  • 17
  • 21