javax.servlet.http
Class Cookie

java.lang.Object
  extended by javax.servlet.http.Cookie
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
CookieImpl

public class Cookie
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

Encapsulates HTTP cookies.

Cookies are use to keep track of users for sessions and to recognize users back for personalization.

When deleting cookies or changing the time, it's important to set the same domain and path as originally passed. Browsers distinguish cookies with different domain and path.

 Cookie myCookie = new Cookie("mycookie", "myvalue");
 myCookie.setPath("/path");
 myCookie.setDomain("mydom.com");
 // will live for a month
 myCookie.setMaxAge(60 * 24 * 3600);
 response.addCookie(myCookie);
 

To delete the above cookie, you'll need to do something like the following:

 Cookie myCookie = new Cookie("mycookie", "myvalue");
 myCookie.setPath("/path");
 myCookie.setDomain("mydom.com");
 // kill the cookies
 myCookie.setMaxAge(0);
 response.addCookie(myCookie);
 

See Also:
Serialized Form

Constructor Summary
Cookie(java.lang.String name, java.lang.String value)
          Create a new cookie with the specified name and value.
 
Method Summary
 java.lang.Object clone()
          Returns a clone of the cookie
 java.lang.String getComment()
          Gets the cookie comment
 java.lang.String getDomain()
          Returns the cookie's domain
 int getMaxAge()
          Returns the max age of the cookie in seconds.
 java.lang.String getName()
          Returns the cookie's name.
 java.lang.String getPath()
          Gets the URL path of a cookie.
 boolean getSecure()
          Returns true if the cookie must be over a secure connection.
 java.lang.String getValue()
          Returns the cookie's value.
 int getVersion()
          Returns cookie protocol version.
 boolean isHttpOnly()
          True for HttpOnly request
 void setComment(java.lang.String comment)
          Sets the cookie comment
 void setDomain(java.lang.String domain)
          Sets the cookie domain.
 void setHttpOnly(boolean isHttpOnly)
          True for HttpOnly request
 void setMaxAge(int maxAge)
          Sets the max age of a cookie.
 void setPath(java.lang.String path)
          Sets the URL path of a cookie.
 void setSecure(boolean secure)
          Tells the browser that this cookie should only be passed over a secure connection like SSL.
 void setValue(java.lang.String value)
          Sets the cookie's value.
 void setVersion(int version)
          Sets cookie protocol version, defaulting to 0.
 java.lang.String toString()
          Converts the cookie to a string for debugging.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Cookie

public Cookie(java.lang.String name,
              java.lang.String value)
Create a new cookie with the specified name and value.

Parameters:
name - name of the cookie
value - value of the cookie
Method Detail

setComment

public void setComment(java.lang.String comment)
Sets the cookie comment

Parameters:
comment - comment string

getComment

public java.lang.String getComment()
Gets the cookie comment


setDomain

public void setDomain(java.lang.String domain)
Sets the cookie domain. A site which uses multiple machines, e.g. with DNS round robin, but a single site will want to set the domain.
 cookie.setDomain("yahoo.com");
 

Parameters:
domain - DNS domain name

getDomain

public java.lang.String getDomain()
Returns the cookie's domain


isHttpOnly

public boolean isHttpOnly()
True for HttpOnly request

Since:
Servlet 3.0

setHttpOnly

public void setHttpOnly(boolean isHttpOnly)
True for HttpOnly request

Since:
Servlet 3.0

setMaxAge

public void setMaxAge(int maxAge)
Sets the max age of a cookie. Setting maxAge to zero deletes the cookie. Setting it to something large makes the cookie persistent. If maxAge is not set, the cookie will only last for the session.

Parameters:
maxAge - lifetime of the cookie in seconds.

getMaxAge

public int getMaxAge()
Returns the max age of the cookie in seconds.


setPath

public void setPath(java.lang.String path)
Sets the URL path of a cookie. Normally, cookies will just use the root path.


getPath

public java.lang.String getPath()
Gets the URL path of a cookie.


setSecure

public void setSecure(boolean secure)
Tells the browser that this cookie should only be passed over a secure connection like SSL.


getSecure

public boolean getSecure()
Returns true if the cookie must be over a secure connection.


getName

public java.lang.String getName()
Returns the cookie's name.


setValue

public void setValue(java.lang.String value)
Sets the cookie's value. Normally this will be a random unique string to lookup the cookie in a database.


getValue

public java.lang.String getValue()
Returns the cookie's value.


getVersion

public int getVersion()
Returns cookie protocol version.


setVersion

public void setVersion(int version)
Sets cookie protocol version, defaulting to 0.


clone

public java.lang.Object clone()
Returns a clone of the cookie

Overrides:
clone in class java.lang.Object

toString

public java.lang.String toString()
Converts the cookie to a string for debugging.

Overrides:
toString in class java.lang.Object