Session in servlet in java

Posted by Satyapriyanayak under Others category on | Points: 40 | Views : 939
Session  is a period of users interaction with the server. Whenever a user accesses any page of the server then the server creates session for the user. The server provides a unique id to each session called as session id. The session of the user remains active till the user keeps on interacting with the server. If the user does not interact with the server for a period of 30 min then the session gets destroyed. This period is called as default inactive interval. The server can change the period for default inactive interval. If the user closes the browser then also the session gets destroyed. The server can destroy session of the user explicitly. Servlet can use session of user to create some variables with value. These variables occupy server memory. Users cannot deny creation of session variables. One servlet can create session variables and other servlets can fetch or change the value of session variables.

Creation and reading process of session

1.Servlet must be a sub class of HttpServlet.
2.Create the reference of javax.servlet.http.httpsession by using getSession() method of HttpservletRequest
3.Use set attribute method of Httpsession to create session variables. Use getAttribute() of Httpsession to find value of session variables.

Method of Http session

Public void setAttribute(string variable name,object value)
This create a variable with in the users session

Public object getAttribute(string variable name)
This returns value of the specified session variable. If the specified variable dose not exits in the user session then it returns null.

Public void invalidate();
It destroys all the variables present in users session

Public void setMaxInactiveInterval(long seconds)
It changes the default period of Inactive interval.

Example: - This servlet will accept a set of numbers one by one infinitely and display the summation result below to the form after each submission of the number file

import javax.servlet.*;
import javax.servlet.http.*;

public class summation extends HttpServlet
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
PrintWriter out=res.getWriter();
out.println("<html><body bgcolor='yellow'>");
String s1=req.getParameter("t1");
out.println("<form >");
out.println("<h1>Number<input type='text' name='t1'></h1>");
out.println("<input type='submit' value='summation'>");

int result=0;
HttpSession ses=req.getSession();
Integer s2=(Integer)ses.getAttribute("sum");
if(s1 != null)
if(s2 ==null)
out.println("<h1>Summation is :"+result+"</h1>");


web.xml settings

<?xml version="1.0" encoding="ISO-8859-1"?>





javac -cp servlet-api.jar (for tomcat 6.0)

Run the tomcat
Here test is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.


Comments or Responses

Login to post response