User Tracking Servlet in Java

User tracking

This is a process to keep information about users on 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 available 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 rewriting
  2. Hidden field
  3. Cookie
  4. Session
First, we will discuss URL rewriting, this is a process to create an explicit 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.
In the first file, 1 to 100 numbers will be displayed when a user clicks the number another page will appear showing that the number the user had clicked previously. file
  1. import javax.servlet.http.*;  
  2. import javax.servlet.*;  
  3. import*;  
  4. public class first extends HttpServlet  
  5. {  
  6.    public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException  
  7.    {  
  8.        PrintWriter out=res.getWriter();  
  9.        out.println("<h2>");  
  10.        for(int i=1;i<101;i++)  
  11.          out.println("<a href='./second?x="+i+"'>"+i+"</a> " );  
  12.      }  
  13. } file
  1. import javax.servlet.http.*;  
  2. import javax.servlet.*;  
  3. import*;  
  4. public class second extends HttpServlet  
  5. {  
  6.    public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException  
  7.    {  
  8.       PrintWriter out=res.getWriter();  
  9.       String s=req.getParameter("x");  
  10.       out.println("<html><body>");  
  11.       out.println("<h1>"+"You clicked:"+s+"</h1>");  
  12.       out.println("</body></html>");   
  13.    }  
  14. }  
Web.xml settings
  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <!--  
  3. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with  
  4. this work for additional information regarding copyright ownership.  
  6. 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  
  10. 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.  
  12. See the License for the specific language governing permissions and limitations under the License.  
  14. -->  
  15. <web-app xmlns=""  
  16. xmlns:xsi=""  
  17. xsi:schemaLocation=""  
  18. version="2.5">  
  20. <servlet>  
  21. <servlet-name>first</servlet-name>  
  22. <servlet-class>first</servlet-class>  
  23. </servlet>  
  25. <servlet-mapping>  
  26. <servlet-name>first</servlet-name>  
  27. <url-pattern>/first</url-pattern>  
  28. </servlet-mapping>  
  30. <servlet>  
  31. <servlet-name>second</servlet-name>  
  32. <servlet-class>second</servlet-class>  
  33. </servlet>  
  35. <servlet-mapping>  
  36. <servlet-name>second</servlet-name>  
  37. <url-pattern>/second</url-pattern>  
  38. </servlet-mapping>  
  40. </web-app>  
Compile both the files as below
javac -cp servlet-api.jar (for tomcat 6.0)
javac -cp servlet-api.jar (for tomcat 6.0)
User tracking Servlet
Run the tomcat then write the below line in the URL
Here the 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.
User tracking Servlet
Remaining three will be discussed soon.
Thanks for reading