.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

User tracking in servlet

Posted By:satyapriyanayak       Posted Date: April 29, 2014    Points: 200    Category: General    URL: http://www.dotnetspark.com  

In this article we will know User tracking in servlet.
 

User tracking:- This is a process to keep information about users in multiple pages.Whenever user gives information to a servlet  then the destination servlet servlet can find values from the request by using getParameter() method.When the user provides a new request then the values avalable in previous request get lost,this is the nature of 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 about

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 path of the destination with the querystring.Whenever user clicks on hyperlink then destination servlet can fetch values by using getParameter() method of the request.

Example:-In the first file 1 to 100 numbers will be displayed when user will click the number another page will appear showing that number the user has click the number 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)

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend