JSP - SPLessons

Chapter 15

JSP Session Tracking

JSP Session Tracking

JSP Session Tracking – If a web application is capable of remembering a client data during a session across the multiple requests then that web application is called as a stateful Web application. Even though HTTP is a stateless protocol, web applications should be made as stateful web applications. For this, working with the following session tracking or session management techniques is necessary.

Hidden Form Fields

JSP Session Tracking – A web server can send a hidden HTML structure field alongside a unique session ID as follows.

<input type="hidden" name="sessionid" value="54321">

at the point when the form is presented, the predetermined name and value are consequently incorporated into the GET or POST information. Every time when web program sends request back, then session_id value can be utilized to monitor distinctive web programs.


JSP Session Tracking – A webserver can relegate a one of a kind session ID as a treat to every web browser and for resulting requests from the client they can be perceived utilizing the got cookie. This may not be a successful way on the grounds that numerous time program does not bolster a cookie.

URL Rewriting

JSP Session Tracking – One can add some additional information on the end of every URL that distinguishes the session, and the server can relate that session identifier with information it has put away about that session. URL Rewriting is a superior approach to keep up sessions and works for the programs when they don’t support cookies yet here downside is that you would have produce each URL powerfully to allocate a session ID however page is basic static HTML page.

Session Object

JSP Session Tracking – JSP makes utilization of servlet gave HttpSession Interface which gives an approach to distinguish a client crosswise over more than one page request or visit to a Web webpage and to store data about that client.

Of course, JSPs have session tracking empowered and another HttpSession object is instantiated for each new client consequently. Incapacitating session following requires unequivocally turning it off by setting the page order session credit to false as follows.

<%@ page session="false" %>

Session tracking is all about making web application as a stateful web application by remembering client data across the multiple requests during a session. Following are the RealTime Implementation of session tracking/session management. Following is the servlet session tracking link.

Servlet Session Tracking

When the client is sending the request, server gives back response where already session will be stored.

Following is the session tracking example.

<%@ page import="java.io.*,java.util.*" %>
   // Get session creation time.
   Date createTime = new Date(session.getCreationTime());
   // Get last access time of this web page.
   Date lastAccessTime = new Date(session.getLastAccessedTime());

   String title = "Welcome Back to my website";
   Integer visitCount = new Integer(0);
   String visitCountKey = new String("visitCount");
   String userIDKey = new String("userID");
   String userID = new String("SPlesson");

   // Check if this is new comer on your web page.
   if (session.isNew()){
      title = "Welcome to SPlessons";
      session.setAttribute(userIDKey, userID);
      session.setAttribute(visitCountKey,  visitCount);
   visitCount = (Integer)session.getAttribute(visitCountKey);
   visitCount = visitCount + 1;
   userID = (String)session.getAttribute(userIDKey);
   session.setAttribute(visitCountKey,  visitCount);
<title>Session Tracking</title>
<body bgcolor ="skyblue">
<img src="E:/splessons.png"></br>
<h1>Session Tracking</h1>
<table border="1" align="center"> 
<tr bgcolor="#949494">
   <th>Session info</th>
   <td><% out.print( session.getId()); %></td>
   <td>Creation Time</td>
   <td><% out.print(createTime); %></td>
   <td>Time of Last Access</td>
   <td><% out.print(lastAccessTime); %></td>
   <td>User ID</td>
   <td><% out.print(userID); %></td>
   <td>Number of visits</td>
   <td><% out.print(visitCount); %></td>

In this code, when compile the code page will be displayed on he browser, if user refresh the page number of counts will be increased. Where the developer used visitCount.
When compile the code following is the code will be displayed.


  • In the URL phase developer can add a little data to identify the session like sessionid=123.
  • The web server has the ability to send the hidden HTML data along with the session id.