Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default session timeout

 
Haris Karameh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there an easy way to get a default httpsession timeout value inside init().
As begginer I have a several ideas how to do it but they are not simple.
TIA Haris
 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
inside of init() in a servlet?

The easiest way would be to define an init parameter for your servlet in the web.xml:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>package.to.MyServlet</servlet-class>
<init-param>
<param-name>defaultSessionTimeout</param-name>
<param-value>60</param-value>
</init-param>
</servlet>

You would maintain this value to be the same value as the default session timeout (later on in web.xml):
<session-config>
<!-- the time in minutes -->
<session-timeout>60</session-timeout>
</session-config>


Then your servlet would read the value of the its init parameter:
public void init(ServletConfig config)
throws ServletException {
int defaultTimeout = 60; // our default we use when no init parameter;
try {
defaultTimeout = Integer.parseInt(config.getInitParameter("defaultSessionTimeout");
} catch (NumberFormatException ex) { /* empty */ }

// other init related stuff
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic