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 :
SetGetMaxInterval.java
import java.io.IOException; import java.io.PrintWriter; 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 { res.setContentType("text/html"); 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>"); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>maxinterval</display-name> <servlet> <servlet-name>SetGetMaxInterval</servlet-name> <servlet-class>SetGetMaxInterval</servlet-class> </servlet> <servlet-mapping> <servlet-name>SetGetMaxInterval</servlet-name> <url-pattern>/SetGetMaxInterval</url-pattern> </servlet-mapping> </web-app>
Output :
When you will execute the above example you will get the following output :
[ 0 ] Comments