Pr.Pg Next Pg

Cookies Handling tutorials

  • Cookies are small bits of textual information that a Web server sends to a browser and that the browser later returns unchanged when visiting the same Web site or domain.

  • By letting the server read information it sent the client previously, the site can provide visitors with a number of conveniences such as presenting the site the way the visitor previously customized it or letting identifiable visitors in without their having to re-enter a password.

  • The servlet sends cookies to the browser by using the HttpServletResponse objects addCookie method, which adds fields to HTTP response headers to send cookies to the browser, one at a time. The browser is expected to support 20 cookies for each Web server, 300 cookies total, and may limit cookie size to 4 KB each.

  • The browser returns cookies to the servlet by adding fields to HTTP request headers. Cookies can be retrieved from a request by using the HttpServletRequest objects getCookies method. Several cookies might have the same name but different path attributes.

  • Cookies affect the caching of the Web pages that use them. HTTP 1.0 does not cache pages that use cookies created with this class. This class does not support the cache control defined with HTTP 1.1.


Cookies Method

  • Below is the list of useful method which you can use while manipulating cookies in JSP.






public void setDomain(String


This method sets the domain to which cookie applies, for example


public String getDomain()


This method gets the domain to which cookie applies, for example


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 browser shutdown.


public String getName()


This method returns the name of the cookie. The name cannot be changed after creation.


Public void setValue(String


This method sets the value associated with the cookie.


public String getValue()

This method gets the value associated with the 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



This method sets the Boolean value indicating

whether the cookie should only be sent over encrypted (i.e. SSL) connections.


public void setComment(String



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.

Pr.Pg border                                              Next Pg