public class Cookie extends Object
Cookie
is set to 1. The version can be configured
using the setVersion
method. The domain, path,
security, and expiry of the cookie can also be set using their
respective set methods.
The toString
method allows the Cookie
to be converted back into text form. This text form converts the
cookie according to the Set-Cookie header form. This is done so
that a created Cookie
instance can be converted
to a string which can be used as a a HTTP header.
Constructor and Description |
---|
Cookie(String name,
String value)
Constructor of the
Cookie that uses a default
version of 1, which is used by RFC 2109. |
Cookie(String name,
String value,
boolean created)
Constructor of the
Cookie that uses a default
version of 1, which is used by RFC 2109. |
Cookie(String name,
String value,
String path)
Constructor of the
Cookie that uses a default
version of 1, which is used by RFC 2109. |
Cookie(String name,
String value,
String path,
boolean created)
Constructor of the
Cookie that uses a default
version of 1, which is used by RFC 2109. |
Modifier and Type | Method and Description |
---|---|
String |
getDomain()
This returns the domain for this cookie.
|
int |
getExpiry()
This returns the number of seconds a cookie lives for.
|
String |
getName()
This returns the name for this cookie.
|
String |
getPath()
This returns the path for this cookie.
|
String |
getValue()
This returns the value for this cookie.
|
int |
getVersion()
This returns the version for this cookie.
|
boolean |
isNew()
This is used to determine if the cookie is new.
|
boolean |
isProtected()
This is used to determine if the cookie is protected against
cross site scripting.
|
boolean |
isSecure()
This determines whether the cookie is secure.
|
void |
setDomain(String domain)
This enables the domain for this
Cookie to be
set. |
void |
setExpiry(int expiry)
This allows a lifetime to be specified for the cookie.
|
void |
setPath(String path)
This is used to set the cookie path for this cookie.
|
void |
setProtected(boolean protect)
This is used to protect the cookie from cross site scripting
vulnerabilities.
|
void |
setSecure(boolean secure)
This is used to determine if the client browser should send
this cookie over a secure protocol.
|
void |
setValue(String value)
This enables the value of the cookie to be changed.
|
void |
setVersion(int version)
This enables the version of the
Cookie to be
set. |
String |
toClientString()
This will give the correct string value of this cookie.
|
String |
toString()
The
toString method converts the cookie to the
Set-Cookie value. |
public Cookie(String name, String value)
Cookie
that uses a default
version of 1, which is used by RFC 2109. This contains none
of the optional attributes, such as domain and path. These
optional attributes can be set using the set methods.
The name must conform to RFC 2109, which means that it can contain only ASCII alphanumeric characters and cannot have commas, white space, or semicolon characters.
name
- this is the name of this cookie instancevalue
- this is the value of this cookie instancepublic Cookie(String name, String value, boolean created)
Cookie
that uses a default
version of 1, which is used by RFC 2109. This contains none
of the optional attributes, such as domain and path. These
optional attributes can be set using the set methods.
The name must conform to RFC 2109, which means that it can contain only ASCII alphanumeric characters and cannot have commas, white space, or semicolon characters.
name
- this is the name of this cookie instancevalue
- this is the value of this cookie instancecreated
- this determines if the cookie is newpublic Cookie(String name, String value, String path)
Cookie
that uses a default
version of 1, which is used by RFC 2109. This allows the
path attribute to be specified for on construction. Other
attributes can be set using the set methods provided.
The name must conform to RFC 2109, which means that it can contain only ASCII alphanumeric characters and cannot have commas, white space, or semicolon characters.
name
- this is the name of this cookie instancevalue
- this is the value of this cookie instancepath
- the path attribute of this cookie instancepublic Cookie(String name, String value, String path, boolean created)
Cookie
that uses a default
version of 1, which is used by RFC 2109. This allows the
path attribute to be specified for on construction. Other
attributes can be set using the set methods provided.
The name must conform to RFC 2109, which means that it can contain only ASCII alphanumeric characters and cannot have commas, white space, or semicolon characters.
name
- this is the name of this cookie instancevalue
- this is the value of this cookie instancepath
- the path attribute of this cookie instancecreated
- this determines if the cookie is newpublic boolean isNew()
public int getVersion()
public void setVersion(int version)
Cookie
to be
set. By default the version of the Cookie
is
set to 1. It is not advisable to set the version higher
than 1, unless it is known that the client will accept it.
Some old browsers can only handle cookie version 0. This can be used to comply with the original Netscape cookie specification. Version 1 complies with RFC 2109.
version
- this is the version number for the cookiepublic String getName()
Cookie
is for, these values will always be present. These are
mandatory for both the Cookie and Set-Cookie headers.
Because the cookie may be stored by name, the cookie name cannot be modified after the creation of the cookie object.
public String getValue()
Cookie
is for, these values will always be present. These are
mandatory for both the Cookie and Set-Cookie headers.public void setValue(String value)
some value
is perfectly legal. However for maximum compatibility
across the different plaforms such as PHP, JavaScript and
others, quotations should be avoided. If quotations are
required they must be added to the string. For example a
quoted value could be created as "some value"
.value
- this is the new value of this cookie objectpublic boolean isSecure()
public void setSecure(boolean secure)
secure
- if true then the cookie should be securepublic boolean isProtected()
HttpOnly
value
for the cookie. Setting this value ensures that the cookie
is not available to some scripting attacks.public void setProtected(boolean protect)
HttpOnly
value for the
cookie. See RFC 6265 for more details on this value.protect
- this determines if the cookie is protectedpublic int getExpiry()
public void setExpiry(int expiry)
expiry
- the duration in seconds the cookie livespublic String getPath()
toString
or toClientString
is invoked the path will
not be present if the path attribute is null.public void setPath(String path)
/pub/bin
, then requests for the
resource http://hostname:port/pub/bin/README
will be issued with this cookie. The cookie is issued for
all resources in the path and all subdirectories.path
- this is the path value for this cookie objectpublic String getDomain()
toString
or toClientString
is
invoked the domain will not be present if this is null.public void setDomain(String domain)
Cookie
to be
set. The form of the domain is specified by RFC 2109. The
value can begin with a dot, like .host.com
.
This means that the cookie is visible within a specific
DNS zone like www.host.com
. By default this
value is null which means it is sent back to its origin.domain
- this is the domain value for this cookiepublic String toClientString()
public String toString()
toString
method converts the cookie to the
Set-Cookie value. This can be used to send the HTTP header
to a client browser. This uses a format that has been tested
with various browsers. This is required as some browsers
do not perform flexible parsing of the Set-Cookie value.
Netscape and IE-5.0 can't or wont handle Path
it must be path
also Netscape can not handle
the path in quotations such as "/path"
it must
be /path
. This value is never in quotations.
For maximum compatibility cookie values are not transmitted in quotations. This is done to ensure that platforms like PHP, JavaScript and various others that don't comply with RFC 2109 can transparently access the sent cookies.
When setting the expiry time for the cookie it is important
to set the max-age
and expires
attributes so that IE-5.0 and up can understand them. Old
versions of IE do not understand max-age
.
Copyright © 2017. All rights reserved.