GetMaxInterval Session

GetMaxInterval Session

Posted in : Java Posted on : July 6, 2011 at 7:39 PM Comments : [ 0 ]

In this section we will discuss how a maximum time interval of a session can be get.

GetMaxInterval Session

In this section we will discuss how a maximum time interval of a session can be get.

In the example given below to accomplish solution of the above problem I used the HttpSession's method getMaxInactiveInterval() which returns the maximum expired time interval. This time interval is returned in seconds and it tells the servlet container to open this session continue for accessing clients while the time interval is not expired. After expiration of this time interval servlet container invalidates the session. The maximum time interval can also be set by the method setMaxInactiveInterval() this method is also referred to the HttpSession interface. We can set the maximum time interval for the session.

Example :

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
public class SetGetMaxInterval extends HttpServlet
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
PrintWriter pw = res.getWriter();
HttpSession ssn = req.getSession();
ssn.setMaxInactiveInterval(2*60);//sets the maximum time interval for 2 minutes
int time = ssn.getMaxInactiveInterval();
pw.println("<h1>The max interval of this session is set for "+time+" seconds</h1>"); 


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Output :

When you will execute the above example you will get the following output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics