Session Tracking Using Servlet in Java


This article explains session tracking using servlet in Java. The NetBeans IDE is used for creating the sample application.

What is session tracking

Session simply means a specific interval of time. It is also known as session handling, a mechanism used to maintain the state of the user within the lifetime of a session. The Http protocol is a stateless protocol. Each time the user makes a request to the server, the server treats the request as the new request. So we need to maintain the state of a user to recognize a specific user.

 Session Tracking Techniques

The following are the techniques of Session Tracking:

  • User Authentication
  • Hidden Form Field
  • URL Rewriting
  • Persistent Cookies
  • The Session Tracking API.
History of Session Tracking

In HTML there are certain hidden fields that are used by the Programmer to maintain the session. The User session is maintained by the programmer by embedding the user choice into URLs with a long String of appended characters. Later, browser cookies were introduced by Netscape, that can be used by each server to store user related information on the client side.

Session creating and Tracking

Each client session is represented by an instance of a class that implements the javax.servlet.http.HttpSession interface in the standard Servlet API. In this HttpSession objects for the user, a Servlet uses the getSession() method of an HttpServletRequest object. This method takes a boolean argument to specify whether a new session object should be created for the client if no session already exits within the application.

Creating a Session

A session is only created when there is a client and a server. HTTP is a request-response based protocol so the session created by them when there is a client and a server.


We create an app in which we show the user visiting the web page how to find the user visiting on a specific web page.

W need to follow several steps in this app.

Step 1

Open the NetBeans IDE.


Step 2

Now choose "Java web" -> "Web application" as in the following:


Step 3

Type your project name as "UserVisitDemo" as in the following:.


Step 4

Choose your server and version wizard as in the following:


Step 5

Now delete your default "index.jsp" file and create a new "index.html" file and write the following code there.


<!DOCTYPE html>



        <title>TODO supply a title</title>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <meta name="viewport" content="width=device-width">


    <body bgcolor=pink>

        <form method="get">

            <center><h1>Click On below link to go to our Web-Site</h1><br>       

            <a href="UserVisitServlet">Visit</a>




Step 6

Now create a new servlet named "UserVisitServlet" and write the following code there.


import javax.servlet.*;

import javax.servlet.http.*;


public class UserVisitServlet extends HttpServlet {

    int counter = 0;

    public void doGet(HttpServletRequest req, HttpServletResponse response)

            throws ServletException, IOException {


        PrintWriter out = res.getWriter();



        out.println("<body bgcolor=gray>");

        out.println("<center><h1>" + "Since loading, this Web-Site has been visited "

                + counter + " times." + "</h1>");





Step 7

Now check your default "web.xml" file that the same code as in the following exists there.


<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">











Step 8

Now your project is ready to run.

Right-click on the Project menu then choose "Run" as in the following:


Step 9

Now click on Run and see the following output generated:


Step 10

Now click on the given link, after clicking on that link go to the visitor page that we created in the servlet.

In this page you see initially that it shows that you visited that page 1 time.


Step 11

In the manner that the visit count increases on a web-page when we open it again and again. we can do that in our web-page. Now click on the reload button of our browser; you will see below what happens.



You have seen that the visit count increases. Similarly when you reload or open this web page the count is increased again and again. This shows the user-visit of our web-page.