public interface ServerCookie
Interface for HTTP cookies.
This interface provides the same methods as the Servlet class
javax.servlet.http (Java EE 7). It is provided for applications that may
eventually migrate to a web server that supports servlets, but
that initially can get by with a simpler API.
The latest cookie specification is RFC 6265.
-
Method Summary
Modifier and TypeMethodDescriptiondefault void
addToHeaders
(HeaderOps headers) Add this cookie to the Set-Cookie headers.static ServerCookie[]
fetchCookies
(HeaderOps headers) Fetch the cookies provided by a set of HTTP headers.Get the comment for this cookie.Get the domain name for this cookieint
Get the maximum age in seconds for this cookie.getName()
Get the name of this cookiegetPath()
Get the path to which the client should return this cookie;boolean
Indicate if the client is expected to send cookies over a secure connection.getValue()
Get the value of the cookieint
Get the version of the protocol to which this cookie compliesboolean
Determine if the the HTTPOnly attribute is presentstatic ServerCookie
newInstance
(String name, String value) Create a new instance ofServerCookie
.void
setComment
(String comment) Set the comment for this cookie.void
Set the domain attribute.void
setHttpOnly
(boolean httpOnly) Indicates whether or not this cookie is HTTPOnly.void
setMaxAge
(int expire) Set the meaximum age attribute.void
Specify the path on the server that determines when the cookie is visible - when the cookie can be sent to this server.void
setSecure
(boolean secure) Indicate if the client should return a cookie on a secure connection to the server.void
Set the value for this cookie.void
setVersion
(int version) Set the version for the Cookie protocol.
-
Method Details
-
getComment
String getComment()Get the comment for this cookie.- Returns:
- the comment; null if not set
- See Also:
-
getDomain
String getDomain()Get the domain name for this cookie- Returns:
- the domain name; null if not set
- See Also:
-
getMaxAge
int getMaxAge()Get the maximum age in seconds for this cookie.- Returns:
- the maximum age in seconds; -1 (the default) if the cookie will persist until a browser shuts down
- See Also:
-
getName
String getName()Get the name of this cookie- Returns:
- the cookie's name
-
getPath
String getPath()Get the path to which the client should return this cookie;- Returns:
- the path
-
getSecure
boolean getSecure()Indicate if the client is expected to send cookies over a secure connection.- Returns:
- true if the connection should be secure; false otherwise
-
getValue
String getValue()Get the value of the cookiePlease see RFC 6265 for a description of the allowed characters.
- Returns:
- the cookie's value
-
getVersion
int getVersion()Get the version of the protocol to which this cookie complies- Returns:
- 0 for the original Netscape specification; 1 for RFC 2109.
-
isHttpOnly
boolean isHttpOnly()Determine if the the HTTPOnly attribute is present- Returns:
- true if the HTTPOnly attribute is present; false otherwise
-
setComment
Set the comment for this cookie. Please see RFC 6265 for a description of the allowed characters.- Parameters:
comment
- the comment.
-
setDomain
Set the domain attribute.- Parameters:
domain
- the domain name
-
setMaxAge
void setMaxAge(int expire) Set the meaximum age attribute. Given RFC 6265, a -1 indicates that this attribute should not appear in the cookie's Set-Cookie header.- Parameters:
expire
- the time in seconds before this cookie should expire; -1 if it should expire when the client shuts down
-
setPath
Specify the path on the server that determines when the cookie is visible - when the cookie can be sent to this server. The cookie is visible for this path and its subpaths. The URI must include any require '%' encoding so that only printable characters (excluding a ';') may appear. This should not include a query string or fragment.- Parameters:
uri
- the path
-
setSecure
void setSecure(boolean secure) Indicate if the client should return a cookie on a secure connection to the server.- Parameters:
secure
- true if the connection must be secure; false otherwise
-
setValue
Set the value for this cookie.Please see RFC 6265 for a description of the allowed characters.
- Parameters:
value
- the value for this cookie
-
setVersion
void setVersion(int version) Set the version for the Cookie protocol. The Servlet API specification (Jarkarta Servlet 4.0) for Cookie.getVersion() currently states, "Since RFC 2109 is still somewhat new, consider version 1 as exprimental: do not use it yet on production sites." RFC 2109 was wrtten in February, 1997, and the latest specification as of July 2011 is RFC 6265, dated April 2011. Java EE 8 is dated August 2017 and Jarkarta EE, is dated February 2018.- Parameters:
version
- 0 for the original Netscape cookie specification; 1 for RFC 2109
-
setHttpOnly
void setHttpOnly(boolean httpOnly) Indicates whether or not this cookie is HTTPOnly. An HttpOnly cookie is one that a browser may not use in "non-HTTP" APIs such as scripts.- Parameters:
httpOnly
- true for an HTTPOnly cookie; false otherwise
-
fetchCookies
Fetch the cookies provided by a set of HTTP headers. The cookies will be in the same order as in the HTTP headers.- Parameters:
headers
- the headers- Returns:
- the cookies; an empty array if there are none
-
newInstance
Create a new instance ofServerCookie
.- Parameters:
name
- the name of the cookievalue
- the value of the cookie- Returns:
- the new instance of
ServerCookie
-
addToHeaders
Add this cookie to the Set-Cookie headers.- Parameters:
headers
- the headers
-