Session Tracking Using Servlet in Java

Introduction

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.

Example

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.

Fig-1.jpg

Step 2

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

Fig-2.jpg

Step 3

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

Fig-3.jpg

Step 4

Choose your server and version wizard as in the following:

Fig-4.jpg

Step 5

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

index.html

<!DOCTYPE html>

<html>

    <head>

        <title>TODO supply a title</title>

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

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

    </head>

    <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>

        </form>

    </body>

</html>

Step 6

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

UserVisitServlet.java

import java.io.*;

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 {

        res.setContentType("text/html");

        PrintWriter out = res.getWriter();

        counter++;

        out.println("<html>");

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

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

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

        out.println("</body></html>");

 

    }

} 

Step 7

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

web.xml

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

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <servlet>

        <servlet-name>UserVisitServlet</servlet-name>

        <servlet-class>UserVisitServlet</servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name>UserVisitServlet</servlet-name>

        <url-pattern>/UserVisitServlet</url-pattern>

    </servlet-mapping>

 

</web-app>

Step 8

Now your project is ready to run.

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

Fig-5.jpg

Step 9

Now click on Run and see the following output generated:

Fig-6.jpg

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.

Fig-7.jpg

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.

Fig-8.jpg

Note

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.


Similar Articles