0% found this document useful (0 votes)
49 views15 pages

Lecture 42

Uploaded by

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

Lecture 42

Uploaded by

apurvbjp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Web Technology

(KCS-602)
Unit 5
Session Tracking, Cookies

Prepared By
Sachin Kumar Sonker
Assistant Professor,UCER Naini,Allahabad
Cookies in Servlet
• A cookie is a small piece of information that is
persisted between the multiple client
requests.
• A cookie has a name, a single value, and
optional attributes such as a comment, path
and domain qualifiers, a maximum age, and a
version number.
How Cookie works
• By default, each request is considered as a
new request.
• In cookies technique, we add cookie with
response from the servlet.
• So cookie is stored in the cache of the
browser.
• After that if request is sent by the user, cookie
is added with request by default. Thus, we
recognize the user as the old user
Types of Cookie
There are 2 types of cookies in servlets.
 Non-persistent cookie
 Persistent cookie
 Non-persistent cookie
It is valid for single session only. It is removed
each time when user closes the browser.
 Persistent cookie
It is valid for multiple session . It is not removed
each time when user closes the browser. It is
removed only if user logout or signout.
Advantage of Cookies
• Simplest technique of maintaining the state.
• Cookies are maintained at client side.
Disadvantage of Cookies
• It will not work if cookie is disabled from the
browser.
• Only textual information can be set in Cookie
object.
Cookie class
javax.servlet.http.Cookie class provides the
functionality of using cookies. It provides a lot
of useful methods for cookies.
Constructor of Cookie class
Useful Methods of Cookie class
Other methods required for using Cookies

public void addCookie(Cookie ck):method of


HttpServletResponse interface is used to add
cookie in response object.
public Cookie[] getCookies():method of
HttpServletRequest interface is used to return
all the cookies from the browser.
How to create Cookie
Cookie ck=new Cookie("user",“sachin");
//creating cookie object
response.addCookie(ck);
//adding cookie in the response
How to delete Cookie
Cookie ck=new Cookie("user","");
//deleting value of cookie
ck.setMaxAge(0);
//changing the maximum age to 0 seconds
response.addCookie(ck);
//adding cookie in the response
How to get Cookies
Cookie ck[]=request.getCookies();
for(int i=0;i<ck.length;i++)
{
out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());
//printing name and value of cookie
}
Session Tracking in Servlets
• Session simply means a particular interval of
time.
• Session Tracking is a way to maintain state
(data) of an user. It is also known as session
management in servlet.
Session Tracking in Servlets
• Http protocol is a stateless so we need to
maintain state using session tracking
techniques.
• Each time user requests to the server, server
treats the request as the new request. So we
need to maintain the state of an user to
recognize to particular user.
• HTTP is stateless that means each request is
considered as the new request.

You might also like