User tracking
- URL rewriting
- Hidden field
- Cookie
- 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.
Example
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.
First.java file
Second.java file
Web.xml settings
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)
Output
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.
http://localhost:8081/test/first
Remaining three will be discussed soon.
Thanks for reading
- 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> " );
- }
- }
- 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>");
- }
- }
- <?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>

Join the conversation! Your thoughts help the community grow.