Session tracking basics

Session tracking is a process that servlets use to maintain state about the series of requests from users across some period of time.

Session Tracking Basics


Session Tracking

Session tracking is a process that servlets use to maintain state about the series of requests from users across some period of time. In session tracking client first make a request for any servlet, container receives the request and generate a unique session track ID and gives it back to the client along with the response. 

This session tracking ID gets stored on the client machine. when the client request again  for the same request session, then the browser looks for that ID and sends it to the server.  In this case container find the Id and sends back the response to the client. 

Session Tracking can be done in three ways: 
1.Hidden Form Fields
2.URL Rewriting

Hidden form field is sent back to the server when the form is submitted. In hidden form fields the html entry will be like this : <input type ="hidden" name = "name" value="">. This means that when you submit the form, the specified name and value will be get included in get or post method. Here the session ID information would be embedded within the form as a hidden field and submitted with the Http POST command.

URL Rewriting can be used in place where we don't want to use cookies. This is used to maintain the session. In case the browser sends a request then it is always interpreted as a new request because http protocol is a stateless protocol as it is not persistent. 

In the case of session tracking cookies are a way for a server to send some message to a client to store, and for the server to later retrieve its data from that client. Servlets send cookies to clients by adding fields to HTTP response headers. Clients automatically return cookies by adding fields to HTTP request headers.

The code of the program is given below:

package myservlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class ShowSession extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String title = "Session Tracking Example";
HttpSession session = request.getSession(true);
String heading;
Integer count = (Integer)session.getAttribute("count");
if (count == null) {
count = new Integer(0);
heading = "Session Tracking Example";
else {
heading = "Session Tracking Example";
count = new Integer(count.intValue() + 1);
session.setAttribute("count", count);
out.println("</html><head><title>"+title+"</title></head><body>" +
"<h1 align=\"\">" + heading + "</H1>\n" +
"<h2></h2>\n" +
"<table border=1 align=\"\">\n" +
"<tr>"+"<TR bgcolor=\"#9999966\">\n"+

"<th>Time period</th>" +
"</tr>" +
"<tr>" +
"<td>ID</td>" +
"<td>" + session.getId() + "</td>" +
"</tr>" +
"<tr>" +
"<td>Current Time</td>" +
" <td>" + new Date(session.getCreationTime()) + "</td>" +
"</tr>" +
"<tr>" +
"<td>Time of Last Access</td>" +
" <td>" + new Date(session.getLastAccessedTime()) + "</td>" +
"</tr>" +
"<tr>" +
"<td>Number of Previous Accesses</td>" +
" <td>" + count + "</td>" +
"</table>" +

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

web.xml file for this program: 

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns=""
Session tracking Example:
<display-name>Session tracking Example</display-name>

The output of the program is given below:

Download Source Code


Share on Google+Share on Google+

Session tracking basics

Posted on: July 8, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Session tracking basics  

Post your Comment

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