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

 

Servlet Cookies Methods

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

S.N.

Method

Description

1

public void setDomain(String pattern)

 

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

2

public String getDomain()

 

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

3

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.

4

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.

5

public String getName()

 

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

6

Public void setValue(String newValue)

This method sets the value associated with the cookie.

7

public String getValue()

This method gets the value associated with the cookie.

8

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.

9

public String getPath()

This method gets the path to which this cookie applies.

10

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.

11

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.

12

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