User Tracking Servlet in Java


User tracking

This is a process to keep information about users in multiple pages. Whenever a user gives information to a servlet then the destination servlet can find values from the request by using the getParameter() method.  When the user provides a new request then the values avalable in the previous request get lost, this is the nature of a stateless protocol. HTTP is a stateless protocol.  User tracking helps to keep information about users even if the request gets changed.  This can be done is four different ways given below:

  1. URL rewritting
  2. Hidden field
  3. Cookie
  4. Session

First we will discuss URL rewritting this is a process to create an explict querystring with the help of a hyperlink. The href attribute of <a> tag can contain a path of the destination with the querystring. Whenever a user clicks on hyperlink then the destination servlet can fetch values by using the getParameter() method of the request.

Example: In the first file 1 to 100 numbers will be displayed when a user clicks the number another page will appear showing that number the user had clicked previously.

First.java file

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class first extends HttpServlet
{
   public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
 PrintWriter out=res.getWriter();
out.println("<h2>");
  for(int i=1;i<101;i++)
out.println("<a href='./second?x="+i+"'>"+i+"</a>&nbsp;" );
     }
}

Second.java file

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class second extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
String s=req.getParameter("x");
out.println("<html><body>");
out.println("<h1>"+"You clicked:"+s+"</h1>");
out.println("</body></html>");
}
}

Web.xml settings

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.

The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and limitations under the License.

-->
<web-app 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_2_5.xsd"
version="2.5">

<servlet>
<servlet-name>first</servlet-name>
<servlet-class>first</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>first</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>second</servlet-name>
<servlet-class>second</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>second</servlet-name>
<url-pattern>/second</url-pattern>
</servlet-mapping>

</web-app>

Compile both the files as below

javac -cp servlet-api.jar first.java (for tomcat 6.0)
javac -cp servlet-api.jar second.java (for tomcat 6.0)

User tracking Servlet

Output

Run the tomcat then write the below line in the URL

Here test is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.

http://localhost:8081/test/first

User tracking Servlet

Remaining three will be discussed soon.

Thanks for reading

Up Next
    Ebook Download
    View all
    Learn
    View all