Servlet Session Timeout Setting


Posted in : Servlet Posted on : November 20, 2010 at 4:07 PM Comments : [ 0 ]
Advertisements
In this tutorial you will learn how to set session time out in servlet

Session Time Out Example

You can set the session time out in two ways either in web.xml file or in servlet.

You can set session time out  in web.xml as

<session-config>
<session-timeout>30</session-timeout>
</session-config>

Here 30 minutes is the maximum time out.

To set session time out in servlet you need to call session.getMaxInactiveInterval(300). Here 300 second is the time out .

An example is given below which illustrates the above concepts.

login.html

<HTML>
<HEAD>
<TITLE> Login Page </TITLE>
</HEAD>
<BODY>
<BODY bgcolor=lightblue>
<form name=login method="get" 
action="http://localhost:8080/sessiontimeout/timeout">
<center>
<h1>Enter your Name and Pasword </h1><br>
<table border=1>
<tr><td>Enter Your Name :</td>
<td><input type="text" name="userName" value=""></td>
<tr><td align=center><input type="submit" name="Submit" value="Submit" ></td>
<td align=center><input type="reset" name="reset" value="Refresh"></td>
<table>
</center>
</form>
</BODY> 
</HTML>

SessionTimeout.java

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionTimeout extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String name = request.getParameter("userName");
if (name.equalsIgnoreCase("web")) {
out.println("<Center><h3>");
out.println("Creating A New Session...........");
out.println("</h3></Center>");
HttpSession session = request.getSession(true);

//Setting Session Inactive Time Out
session.setMaxInactiveInterval(20);

session.setAttribute("loggedUser", name);
out.println("<Center><h3>");
out.println("Session Created");
out.println("<br>Session Id - ");
out.println(session.getId());
out.println("<a href=\"http://localhost:8080/sessiontimeout/check\">Check Time Out</a>");
out.println("</h3></Center>");
} else {
out.println("<h1>");
out.println("Please enter the name \"web\"");
out.println("<h1>");
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}

TimeoutCheck.java

package roseindia.net;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class TimeoutCheck extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String name = null;
HttpSession session = request.getSession(false);
if (session != null) {
name = (String) session.getAttribute("loggedUser");
out.println("<center><h1>");
out.println("Logged User is - ");
out.println(name);
out.println("</h1></center>");
} else {
out.println("<center><h1>");
out.println("Session Session Time Out");
out.println("</h1></center>");
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
}
}

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>CreateSession</display-name>
<!-- Servlet -->

<servlet>
<servlet-name>SessionTimeout</servlet-name>
<servlet-class>roseindia.net.SessionTimeout</servlet-class>
</servlet>
<servlet>
<servlet-name>TimeoutCheck</servlet-name>
<servlet-class>roseindia.net.TimeoutCheck</servlet-class>
</servlet>

<!-- Servlet Mapping-->

<servlet-mapping>
<servlet-name>SessionTimeout</servlet-name>
<url-pattern>/timeout</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TimeoutCheck</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping>

<!-- Setting Session Time Out-->
<session-config>
<session-timeout>2</session-timeout> 
</session-config>

</web-app>

When you run this application it will display message as shown below:

 

After 20 Seconds

Download this example code

Go to Topic «PreviousHomeNext»

Your Comment:


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