public class Cookie
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable
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);
Constructor and Description |
---|
Cookie(java.lang.String name,
java.lang.String value)
Create a new cookie with the specified name and value.
|
Modifier and Type | Method and Description |
---|---|
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.
|
public Cookie(java.lang.String name, java.lang.String value)
name
- name of the cookievalue
- value of the cookiepublic void setComment(java.lang.String comment)
comment
- comment stringpublic java.lang.String getComment()
public void setDomain(java.lang.String domain)
cookie.setDomain("yahoo.com");
domain
- DNS domain namepublic java.lang.String getDomain()
public boolean isHttpOnly()
public void setHttpOnly(boolean isHttpOnly)
public void setMaxAge(int maxAge)
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.maxAge
- lifetime of the cookie in seconds.public int getMaxAge()
public void setPath(java.lang.String path)
public java.lang.String getPath()
public void setSecure(boolean secure)
public boolean getSecure()
public java.lang.String getName()
public void setValue(java.lang.String value)
public java.lang.String getValue()
public int getVersion()
public void setVersion(int version)
public java.lang.Object clone()
clone
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object