Demonstrate session tracking in Servlet

Q. Write an application to demonstrate the session tracking in Servlet.

Answer:

Session is basically a time frame and tracking means maintaining user data for certain period of time frame. Session TrackingĀ is a mechanism used by the web container to store session information for a particular user. It is used to recognize a particular user.

Below example shows how HttpSession object finds out creation time and last accessed time for a session.

SessionDemo.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

// Extend HttpServlet class
public class SessionDemo extends HttpServlet
{
     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
     {
           // Create a session object if it is already not  created.
           HttpSession session = request.getSession(true);
           // 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("Surendra");

           // Check if this is new comer on your web page.
           if (session.isNew())
          {
               title = "Welcome to my website";
               session.setAttribute(userIDKey, userID);
          }
          else
          {
               visitCount = (Integer)session.getAttribute(visitCountKey);
               visitCount = visitCount + 1;
               userID = (String)session.getAttribute(userIDKey);
          }
          session.setAttribute(visitCountKey,  visitCount);

          // Set response content type
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();

          String docType =
          "<!doctype html public \"-//w3c//dtd html 4.0 " +
          "transitional//en\">\n";

           out.println(docType +
               "<html>\n" +
               "<head><title>" + title + "</title></head>\n" +
               "<body bgcolor=\"#e5f7c0\">\n" +
               "<h1 align=\"center\">" + title + "</h1>\n" +
               "<h2 align=\"center\">Session Infomation</h2>\n" +
               "<table border=\"1\" align=\"center\">\n" +
               "<tr bgcolor=\"#eadf8c\">\n" +
               "<th>Session info</th><th>value</th></tr>\n" +
               "<tr>\n" +
               "  <td>id</td>\n" +
               "  <td>" + session.getId() + "</td></tr>\n" +
               "<tr>\n" +
               "  <td>Creation Time</td>\n" +
               "  <td>" + createTime +
               "  </td></tr>\n" +
               "<tr>\n" +
               "  <td>Time of Last Access</td>\n" +
               "  <td>" + lastAccessTime +
               "  </td></tr>\n" +
               "<tr>\n" +
               "  <td>User ID</td>\n" +
               "  <td>" + userID +
               "  </td></tr>\n" +
               "<tr>\n" +
               "  <td>Number of visits</td>\n" +
               "  <td>" + visitCount + "</td></tr>\n" +
               "</table>\n" +
               "</body></html>");
     }
}


web.xml

<web-app>
     <servlet>
          <servlet-name>abc</servlet-name>
          <servlet-class>SessionDemo</servlet-class>
     </servlet>
     <servlet-mapping>
          <servlet-name>abc</servlet-name>
          <url-pattern>/test</url-pattern>
     </servlet-mapping>
</web-app>


Output:

i. When we run the application first time

session tracking

ii. After refreshing the application

session tracking