Cookies in Servlet

Cookies are text files that are sent by Servlet to the Web Browsers and are used to track various information's about the user.

Cookies in Servlet


Cookies in Servlet

Cookies are text files that are sent by Servlet to the Web Browsers that uniquely identifies a client. Browsers store cookies on local computer.

Cookies are used to track various informations. Whenever a browser sends a request to browser, it also sends the information stored in the local computer so that server can identify the user.

A cookie has a name, a value, optional attributes and a version number.

HttpServletResponse.addCookie(javax.servlet.http.Cookie) method is used by servlet to send cookies to the browser

A browser can store up to 20 cookies for each Web server and a total of 300 cookies.

HttpServletRequest.getCookies() method is used to retrieve cookies.

Cookies can have same name but they always have different path attributes.

Cookies affect the Web pages cache.

Servlet Cookie Methods:

java.lang.Object clone(): overrides java.lang.Object.clone method and returns a copy of this cookie

java.lang.String getComment(): returns the comment attached to this cookie

java.lang.String getDomain(): returns the domain name set for this cookie

int getMaxAge(): returns the maximum age of the cookie in seconds

java.lang.String getName(): returns the cookie's name

java.lang.String getPath(): returns the path on the server where the browser will return this cookie

boolean getSecure(): if the browser sends cookies on a secure protocol, it returns true else false

java.lang.String getValue(): returns cookie's value

int getVersion(): returns the version of the protocol cookie follows

void setComment(java.lang.String purpose): specifies cookie's purpose

void setDomain(java.lang.String pattern): specifies the domain in which cookie must be presented

void setMaxAge(int expiry): sets cookie's maximum age in seconds

void setPath(java.lang.String uri): specifies a path for the client to return the cookie

void setSecure(boolean flag): indicates that the cookie must be sent using a secure protocol

void setValue(java.lang.String newValue): When a cookie has been created, it assigns new value to cookie.

void setVersion(int v): Sets the version of the cookie.