0% found this document useful (0 votes)
26 views7 pages

Servlets Part 7

The document discusses session tracking in servlets, outlining four methods: Cookies, URL Rewriting, Http Sessions, and Hidden Form Fields. It provides detailed examples of using Cookies and URL Rewriting to maintain client state information, along with comparisons of their advantages and disadvantages. The document includes code snippets for setting and retrieving cookies and URL parameters in Java servlets.

Uploaded by

manoj sanikala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

Servlets Part 7

The document discusses session tracking in servlets, outlining four methods: Cookies, URL Rewriting, Http Sessions, and Hidden Form Fields. It provides detailed examples of using Cookies and URL Rewriting to maintain client state information, along with comparisons of their advantages and disadvantages. The document includes code snippets for setting and retrieving cookies and URL parameters in Java servlets.

Uploaded by

manoj sanikala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

venkatesh.mansani@yahoo.

com Naresh i Technologies

servlets part-7
Session Tracking:
Session tracking is a mechanism that servlets use to maintain client state
information about a series of request from the same user across some time
period.
Client state information can be a user name, password, shopping items,
examination id, .. etc.,

There are four session tracking methods:


1) Cookies
2) URL Rewriting
3) Http Sessions
4) Hidden Form Fields

1) Cookies:
A cookie is a piece of information stored at client side to maintain client state
information.

Example:
books.html
<html>
<body bgcolor=green text=yellow>
<h1><u>Java Books</u></h1>
<form action=set>
<input type=checkbox name=book1 value=Java2CompleteReference> Java 2
Complete Reference<br>
<input type=checkbox name=book2 value=HeadFirstJava> Head First Java<br>

Java By Venkatesh Mansani [email protected]


[email protected] Naresh i Technologies

<input type=checkbox name=book3 value=SCJPByKathySierra> SCJP By Kathy


Sierra<br><br>
<input type=submit><input type=reset>
</form>
</body>
</html>
SetCookie.java
package cookie;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/set")
public class SetCookie extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String s1=request.getParameter("book1");
String s2=request.getParameter("book2");
String s3=request.getParameter("book3");
if(s1!=null)
{

Java By Venkatesh Mansani [email protected]


[email protected] Naresh i Technologies

Cookie c1=new Cookie("b1",s1);


response.addCookie(c1);
}
if(s2!=null)
{
Cookie c2=new Cookie("b2",s2);
response.addCookie(c2);
}
if(s3!=null)
{
Cookie c3=new Cookie("b3",s3);
response.addCookie(c3);
}
PrintWriter pw=response.getWriter();
pw.println("<html><body bgcolor=cyan text=red>");
pw.println("<h1>Your Books Are Added To Cart</h1>");
pw.println("<a href=get>Next</a>");
pw.println("</body></html>");
}
}
GetCookie.java
package cookie;
import java.io.IOException;
import java.io.PrintWriter;

Java By Venkatesh Mansani [email protected]


[email protected] Naresh i Technologies

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/get")
public class GetCookie extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
PrintWriter pw=response.getWriter();
pw.println("<html><body bgcolor=yellow text=blue>");
pw.println("<h1>Selected Books:</h1>");
Cookie[] c1=request.getCookies();
for(Cookie c2 : c1)
{
String s=c2.getValue();
pw.println(s+"<br>");
}
pw.println("</body></html>");
}
}

2) URL Rewriting:
In this session tracking method client state information appended to URL.

Java By Venkatesh Mansani [email protected]


[email protected] Naresh i Technologies

Example:
Set.java
package url;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/set")
public class Set extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String s1=request.getParameter("book1");
String s2=request.getParameter("book2");
String s3=request.getParameter("book3");
PrintWriter pw=response.getWriter();
pw.println("<html><body bgcolor=cyan text=red>");
pw.println("<h1>Your Books Are Added To Cart</h1>");
pw.println("<a
href=get?b1="+s1+"&b2="+s2+"&b3="+s3+">Next</a>");
pw.println("</body></html>");
}
}

Java By Venkatesh Mansani [email protected]


[email protected] Naresh i Technologies

Get.java
package url;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/get")
public class Get extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
PrintWriter pw=response.getWriter();
String s1=request.getParameter("b1");
String s2=request.getParameter("b2");
String s3=request.getParameter("b3");
pw.println("<html><body bgcolor=yellow text=blue>");
pw.println("<h1>Selected Books:</h1>");
if(!(s1.equals("null")))
{
pw.println(s1);
}
if(!(s2.equals("null")))

Java By Venkatesh Mansani [email protected]


[email protected] Naresh i Technologies

{
pw.println(s2);
}
if(!(s3.equals("null")))
{
pw.println(s3);
}
pw.println("</body></html>");
}
}

Cookies Vs URL Rewriting


Cookies URL Rewriting
1) In this session tracking method client 1) In this session tracking method
client state information stored at client side. client state information appended
to URL.
2) It supports only text. 2) It is also supports only text.
3) Here size of the data is limited. 3) Here also size of the data is
limited.
4) Here it is possible to set the time interval. 4) Here it is not possible to set the
time interval.
5) Cookies are not secure because cookies 5) It is also not secure because here
client state information can be viewed by client state information displayed
the user through browser settings option. in address bar in a browser window.
6) This session tracking method fails if the 6) This session tracking method
cookies are disabled in a browser. always works.

Java By Venkatesh Mansani [email protected]

You might also like