This is a process to keep information about the 
users even if the request gets changed. Whenever a user provides a new request 
then the values of form fields available in a previous request get lost. It helps 
to keep values of form fields available in a previous request in the current 
request. This can be done 4 different ways:
- URL rewriting
 - Hiden field
 - Session
 - Cookies
 
URL rewriting: - This is a process to 
create an explicit query string with the help of a hyperlink.
Example
<html>
<body>
<%
String s=request.getParameter("x");
%>
<h2><a href="?x=New Delhi">India</a>
<h2><a href="?x=Colombo">Srilanka</a>
<%
if(s!=null)
{
%>
<h1>Capital is: <%= s%></h1>
<%
}
%>
</body>
</html>
Output
![user tracking in java]()
![jsp]()
Hidden field: - This is a process to store information present in the 
current request in a form field called a hidden field. Whenever a form containing a hidden field gets submitted then the destination jsp can find the value of hidden 
fields by using the getParameter() method of the request.
Example:- Addition of two numbers
<html>
<head>
<script language="javascript">
function validate(objForm){
if(objForm.t1.value.length==0){
alert("Please enter a Number!");
objForm.t1.focus();
return false;
}
if(objForm1.t2.value.length==0){
alert("Please enter a Number!");
objForm.t2.focus();
return false;
}
return true;
}
</script>
<body>
<%
String s1=request.getParameter("t1");
String s2=request.getParameter("t2");
if(s1 ==null)
{
%>
<form name ="objForm" onSubmit="return validate(this)">
<h1>Enter a Number<input type='text' name='t1'></h1>
<input type='submit' value='next' >
</form>
<%
}
else if(s2 ==null)
{
%>
<form name ="objForm1" onSubmit="return validate(this)">
<h1>Enter a Number<input type='text' name='t2'></h1>
<input type='hidden' name='t1' value=<%=s1%>>
<input type="submit" value="Add">
</form>
<%
}
else
{
int x=Integer.parseInt(s1);
int y=Integer.parseInt(s2);
%>
<h1>Addition is: <font color='Red'><%= (x+y) %></font> </h1>
<%
}
%>
</body>
</html>
Output
![java user tracking]()
Cookie: - It is a memory location available in user's machine. This memory 
location can be used by the server to store information about the user as the 
value of some variables. JSP can send cookie variables to the user as a part 
of its response. Other JSPs can fetch value of cookie variable from the request. 
The User can deny the creation process of the cookie variable. If the user denies or delete cookie variables then the other pages 
cannot find the value of those variables. 
Example
Product.html
<html>
<body bgcolor="red">
<form action="./create.jsp" method="get">
<h2>Product Name<input type="text" name="t1"></h2>
<input type="submit" value="submit">
</form>
</body>
</html>
create.jsp 
<html>
<body bgcolor="pink">
<%
String s=request.getParameter("t1");
Cookie c=new Cookie("prodn",s);
response.addCookie(c);
%>
<h1>Product is added </h1>
<h2><a href="./buy.jsp">Buy now</a></h2>
</body>
</html>
buy.jsp
<html>
<body bgcolor="pink">
<%
Cookie c[]=request.getCookies();
String str=null;
for(int i=0;i<c.length;i++)
{
if(c[i].getName().equals("prodn"))
str=c[i].getValue();
}
%>
<h1>Ur product <font color='Red'><%=str %></font> will be delivered soon</h1>
</body>
</html>
Output
![jsp user tracking]()
Session: - It is a period of a user's interaction with the server. When the 
user accesses any page of the server then the server creates a session for the user 
by allocating a session ID. The session remains active while the user keeps on 
interacting with the server. JSP can use the session of the user to store 
information about the user. One jsp page can store information in the session 
variable and other pages can access values of the session variables.
Using the session object
This is an object available to JSP as an implicit object. This is an object of type Javax.servlet.http.Httpsession. This object is declared in the servlet present 
behind jsp. Session variables can be created by using the setAttribute() method 
upon the session object. The value of a session variable can be fetched by using the  getAttribute() method on the session object.
Example
Product.jsp
<html>
<head>
<script language="javascript">
function validate(objForm)
{
if(objForm.t1.value.length==0)
{
alert("Please enter a Product!");
objForm.t1.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name="objForm" onSubmit="return validate(this)">
<h1>Enter product name<input type="text" name="t1"></h1>
<input type="submit" value="Add to cart">
</form>
<%
String s1=request.getParameter("t1");
//to put value into session as product names
if(s1!=null && !s1.equals(""))
{
String str=(String)session.getAttribute("pn");
if(str==null)
str=s1;
else
str=str+" "+s1;
session.setAttribute("pn",str);
out.println("<h3>Product list:");
out.println("<li>" +str+ "</li>");
}
%>
</body>
</html>
![user tracking in jsp]()
Thanks for reading