.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 jsp

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

In this article we will know about the user tracking in Jsp.
 

This is a process to keep information about the users even if the request gets changed. Whenever user provides a new request then the values of form fields available in previous request get lost. It helps to keep values of form fields available in previous request into current request. This can be done by using 4 different ways they are below

1.URL rewriting
2.Hiden field
3.Session
4.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>

 
Hidden field: - This is a process to store information present in the current request in a form field called as hidden field. Whenever form containing hidden field gets submitted then the destination jsp can find value of hidden field by using 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>

Cookie: - It is a memory location available in users 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 the 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 present in user machine. If the user denies or delete cookie variables then the other pages cannot find 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>


Session: - It is a period of user interaction with the server. When the user access any page of the server then the server creates session for the user by allocating session Id .The session remains active till the user keeps on interacting with the server .Jsp can use session of the user to store information about the user. One jsp can store information in the session variable and other pages can access values of the session variables.

Using session object

This is an object available to Jsp as an implicit object. This is an object of Javax.servlet.http.Httpsession.This object is declared in the servlet present behind the jsp. Session variables can be created by using setAttribute() method upon the session object. Value of a session variable can be fetched by using getAttribute() method upon 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>


 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