0% found this document useful (0 votes)
105 views5 pages

Cookies

Uploaded by

Bhavesh kumar
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)
105 views5 pages

Cookies

Uploaded by

Bhavesh kumar
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

Cookies

Introduction
 Cookies are text files stored on the client computer and
they are kept for various information tracking purposes.
JSP transparently supports HTTP cookies using
underlying servlet technology.
 There are three steps involved in identifying and
returning users −
◦ Server script sends a set of cookies to the browser. For
example, name, age, or identification number, etc.
◦ Browser stores this information on the local machine for future
use.
◦ When the next time the browser sends any request to the web
server then it sends those cookies information to the server
and server uses that information to identify the user or may be
for some other purpose as well.
Methods
 public void setDomain(String pattern): This method sets the domain
to which the cookie applies.
 public String getDomain(): This method gets the domain to which the
cookie applies.
 public void setMaxAge(int expiry): This method sets how much time
(in seconds) should elapse before the cookie expires. If you don't set
this, the cookie will last only for the current session.
 public int getMaxAge(): This method returns the maximum age of
the cookie, specified in seconds, By default, -1 indicating the cookie
will persist until the browser shutdown.
 public String getName(): This method returns the name of the
cookie. The name cannot be changed after the creation.
 public void setValue(String newValue): This method sets the value
associated with the cookie.
 public String getValue(): This method gets the value associated with
the cookie.
Setting Cookies with JSP

 Step 1: Creating a Cookie object


Cookies c=new Cookies(“key”,”value”);
Keep in mind, neither the name nor the value should
contain white space or any of the following characters −
[]()=,"/?@:;
 Step 2: Setting the maximum age

You use setMaxAge to specify how long (in seconds) the


cookie should be valid. The following code will set up a
cookie for 24 hours.
[Link](60*60*24);
 Step 3: Sending the Cookie into the HTTP response

headers
[Link](cookie);
 public void setPath(String uri): This method sets the path to which this
cookie applies. If you don't specify a path, the cookie is returned for all URLs
in the same directory as the current page as well as all subdirectories.

 public String getPath(): This method gets the path to which this cookie
applies.

 public void setSecure(boolean flag): This method sets the boolean value
indicating whether the cookie should only be sent over encrypted (i.e, SSL)
connections.

 public void setComment(String purpose): This method specifies a comment


that describes a cookie's purpose. The comment is useful if the browser
presents the cookie to the user.

 public String getComment(): This method returns the comment describing


the purpose of this cookie, or null if the cookie has no comment.

You might also like