27. How to handle cookies in JSP

  • The getCookies() method of the request object returns an array of Cookie objects.
  • Cookies are used to allow web browsers to hold small amounts of information or state data associated with a user’s web browsing.
  • Cookies are named and have a single value. They may have optional attributes, including a comment presented to the user, path and domain qualifiers for which hosts see the cookie, a maximum age, and a version.
 
Cookie(java.lang.String name, java.lang.String value)

Cookie objects have the following methods.

getComment()
Returns the comment describing the purpose of this cookie, or null if no such comment has been defined.

getMaxAge()
Returns the maximum specified age of the cookie.

getName()
Returns the name of the cookie.

getPath()
Returns the prefix of all URLs for which this cookie is targetted.

getValue()
Returns the value of the cookie.

setComment(String)
If a web browser presents this cookie to a user, the cookie’s purpose will be described using this comment.

setMaxAge(int)
Sets the maximum age of the cookie. The cookie will expire after that many seconds have passed. Negative values indicate the default behaviour: the cookie is not stored persistently, and will be deleted when the user web browser exits. A zero value causes the cookie to be deleted

setPath(String)
This cookie should be presented only with requests beginning with this URL.

setValue(String)
Sets the value of the cookie. Values with various special characters (white space, brackets and parentheses, the equals sign, comma, double quote, slashes, question marks, the “at” sign, colon, and semicolon) should be avoided. Empty values may not behave the same way on all browsers.

Advertisements

One Response

  1. thank you….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: