FAQ


Advertisement



How to handle cookies in JSP?

What is Cookie?

Cookies are stored on the server and referenced with a session ID that is passed back and forth between the client and the server. Cookies are a general mechanism used by server-side applications to store information in individual browsers. The server-side application can retrieve cookies stored in a browser. With cookies, your web application can create specific variables for each browser. Such variables might include a user name or the last-accessed date. Cookies provide a persistence mechanism that you can use to compensate for the HTTP protocol stateless nature.

How to send and retrive 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. Common applications for cookies include storing user preferences, automating low security user "sign on" facilities, and helping collect data used for "shopping cart" style applications.

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. The following shows constrtucting a cookie with a specified name and value.

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.

Sending cookies to the browser can be done by the addCookie() method on the response object.

For example,

<%
out.println("Cookies received by the server:
");

Cookie[] myCookies = request.getCookies();

for(int n=0; n < myCookies.length; n++)
{
out.print("[" + myCookies[n].getName() + "] = ");
out.print(myCookies[n].getValue() + ";
");
}

out.println("Cookies added by the server:
");
Cookie objCookie = new Cookie("domain", "www.xyzws.com");
objCookie.setPath("/");
objCookie.setMaxAge(60*60*24*365);
response.addCookie(objCookie);
....
%>


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |