- java.lang.Object
-
- javax.ws.rs.core.Cookie
-
- javax.ws.rs.core.NewCookie
-
public class NewCookie extends Cookie
Used to create a new HTTP cookie, transferred in a response.- Since:
- 1.0
- Author:
- Paul Sandoz, Marc Hadley
- See Also:
- IETF RFC 2109
-
-
Field Summary
Fields Modifier and Type Field and Description static int
DEFAULT_MAX_AGE
Specifies that the cookie expires with the current application/browser session.-
Fields inherited from class javax.ws.rs.core.Cookie
DEFAULT_VERSION
-
-
Constructor Summary
Constructors Constructor and Description NewCookie(Cookie cookie)
Create a new instance copying the information in the supplied cookie.NewCookie(Cookie cookie, String comment, int maxAge, boolean secure)
Create a new instance supplementing the information in the supplied cookie.NewCookie(Cookie cookie, String comment, int maxAge, Date expiry, boolean secure, boolean httpOnly)
Create a new instance supplementing the information in the supplied cookie.NewCookie(String name, String value)
Create a new instance.NewCookie(String name, String value, String path, String domain, int version, String comment, int maxAge, boolean secure)
Create a new instance.NewCookie(String name, String value, String path, String domain, int version, String comment, int maxAge, Date expiry, boolean secure, boolean httpOnly)
Create a new instance.NewCookie(String name, String value, String path, String domain, String comment, int maxAge, boolean secure)
Create a new instance.NewCookie(String name, String value, String path, String domain, String comment, int maxAge, boolean secure, boolean httpOnly)
Create a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description boolean
equals(Object obj)
Compare for equality.String
getComment()
Get the comment associated with the cookie.Date
getExpiry()
Get the cookie expiry date.int
getMaxAge()
Get the maximum age of the the cookie in seconds.int
hashCode()
Generate a hash code by hashing all of the properties.boolean
isHttpOnly()
Returnstrue
if this cookie contains theHttpOnly
attribute.boolean
isSecure()
Whether the cookie will only be sent over a secure connection.Cookie
toCookie()
Obtain a new instance of aCookie
with the same name, value, path, domain and version as thisNewCookie
.String
toString()
Convert the cookie to a string suitable for use as the value of the corresponding HTTP header.static NewCookie
valueOf(String value)
Creates a new instance of NewCookie by parsing the supplied string.-
Methods inherited from class javax.ws.rs.core.Cookie
getDomain, getName, getPath, getValue, getVersion
-
-
-
-
Field Detail
-
DEFAULT_MAX_AGE
public static final int DEFAULT_MAX_AGE
Specifies that the cookie expires with the current application/browser session.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
NewCookie
public NewCookie(String name, String value)
Create a new instance.- Parameters:
name
- the name of the cookie.value
- the value of the cookie.- Throws:
IllegalArgumentException
- if name isnull
.
-
NewCookie
public NewCookie(String name, String value, String path, String domain, String comment, int maxAge, boolean secure)
Create a new instance.- Parameters:
name
- the name of the cookie.value
- the value of the cookie.path
- the URI path for which the cookie is valid.domain
- the host domain for which the cookie is valid.comment
- the comment.maxAge
- the maximum age of the cookie in seconds.secure
- specifies whether the cookie will only be sent over a secure connection.- Throws:
IllegalArgumentException
- if name isnull
.
-
NewCookie
public NewCookie(String name, String value, String path, String domain, String comment, int maxAge, boolean secure, boolean httpOnly)
Create a new instance.- Parameters:
name
- the name of the cookie.value
- the value of the cookie.path
- the URI path for which the cookie is valid.domain
- the host domain for which the cookie is valid.comment
- the comment.maxAge
- the maximum age of the cookie in seconds.secure
- specifies whether the cookie will only be sent over a secure connection.httpOnly
- iftrue
make the cookie HTTP only, i.e. only visible as part of an HTTP request.- Throws:
IllegalArgumentException
- if name isnull
.- Since:
- 2.0
-
NewCookie
public NewCookie(String name, String value, String path, String domain, int version, String comment, int maxAge, boolean secure)
Create a new instance.- Parameters:
name
- the name of the cookievalue
- the value of the cookiepath
- the URI path for which the cookie is validdomain
- the host domain for which the cookie is validversion
- the version of the specification to which the cookie compliescomment
- the commentmaxAge
- the maximum age of the cookie in secondssecure
- specifies whether the cookie will only be sent over a secure connection- Throws:
IllegalArgumentException
- if name isnull
.
-
NewCookie
public NewCookie(String name, String value, String path, String domain, int version, String comment, int maxAge, Date expiry, boolean secure, boolean httpOnly)
Create a new instance.- Parameters:
name
- the name of the cookievalue
- the value of the cookiepath
- the URI path for which the cookie is validdomain
- the host domain for which the cookie is validversion
- the version of the specification to which the cookie compliescomment
- the commentmaxAge
- the maximum age of the cookie in secondsexpiry
- the cookie expiry date.secure
- specifies whether the cookie will only be sent over a secure connectionhttpOnly
- iftrue
make the cookie HTTP only, i.e. only visible as part of an HTTP request.- Throws:
IllegalArgumentException
- if name isnull
.- Since:
- 2.0
-
NewCookie
public NewCookie(Cookie cookie)
Create a new instance copying the information in the supplied cookie.- Parameters:
cookie
- the cookie to clone.- Throws:
IllegalArgumentException
- if cookie isnull
.
-
NewCookie
public NewCookie(Cookie cookie, String comment, int maxAge, boolean secure)
Create a new instance supplementing the information in the supplied cookie.- Parameters:
cookie
- the cookie to clone.comment
- the comment.maxAge
- the maximum age of the cookie in seconds.secure
- specifies whether the cookie will only be sent over a secure connection.- Throws:
IllegalArgumentException
- if cookie isnull
.
-
NewCookie
public NewCookie(Cookie cookie, String comment, int maxAge, Date expiry, boolean secure, boolean httpOnly)
Create a new instance supplementing the information in the supplied cookie.- Parameters:
cookie
- the cookie to clone.comment
- the comment.maxAge
- the maximum age of the cookie in seconds.expiry
- the cookie expiry date.secure
- specifies whether the cookie will only be sent over a secure connection.httpOnly
- iftrue
make the cookie HTTP only, i.e. only visible as part of an HTTP request.- Throws:
IllegalArgumentException
- if cookie isnull
.- Since:
- 2.0
-
-
Method Detail
-
valueOf
public static NewCookie valueOf(String value)
Creates a new instance of NewCookie by parsing the supplied string.- Parameters:
value
- the cookie string.- Returns:
- the newly created
NewCookie
. - Throws:
IllegalArgumentException
- if the supplied string cannot be parsed or isnull
.
-
getComment
public String getComment()
Get the comment associated with the cookie.- Returns:
- the comment or null if none set
-
getMaxAge
public int getMaxAge()
Get the maximum age of the the cookie in seconds. Cookies older than the maximum age are discarded. A cookie can be unset by sending a new cookie with maximum age of 0 since it will overwrite any existing cookie and then be immediately discarded. The default value of-1
indicates that the cookie will be discarded at the end of the browser/application session.Note that it is recommended to use
Max-Age
to control cookie expiration, however some browsers do not understandMax-Age
, in which case settinggetExpiry()
Expires} parameter may be necessary.- Returns:
- the maximum age in seconds.
- See Also:
getExpiry()
-
getExpiry
public Date getExpiry()
Get the cookie expiry date. Cookies whose expiry date has passed are discarded. A cookie can be unset by setting a new cookie with an expiry date in the past, typically the lowest possible date that can be set.Note that it is recommended to use
Max-Age
to control cookie expiration, however some browsers do not understandMax-Age
, in which case settingExpires
parameter may be necessary.- Returns:
- cookie expiry date or
null
if no expiry date was set. - Since:
- 2.0
- See Also:
getMaxAge()
-
isSecure
public boolean isSecure()
Whether the cookie will only be sent over a secure connection. Defaults tofalse
.- Returns:
true
if the cookie will only be sent over a secure connection,false
otherwise.
-
isHttpOnly
public boolean isHttpOnly()
Returnstrue
if this cookie contains theHttpOnly
attribute. This means that the cookie should not be accessible to scripting engines, like javascript.- Returns:
true
if this cookie should be considered http only,false
otherwise.- Since:
- 2.0
-
toCookie
public Cookie toCookie()
Obtain a new instance of aCookie
with the same name, value, path, domain and version as thisNewCookie
. This method can be used to obtain an object that can be compared for equality with anotherCookie
; since aCookie
will never compare equal to aNewCookie
.- Returns:
- a
Cookie
-
toString
public String toString()
Convert the cookie to a string suitable for use as the value of the corresponding HTTP header.
-
hashCode
public int hashCode()
Generate a hash code by hashing all of the properties.
-
equals
public boolean equals(Object obj)
Compare for equality. UsetoCookie()
to compare aNewCookie
to aCookie
considering only the common properties.
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
21/08/2025 13:03:53 Cette version de la page est en cache (à la date du 21/08/2025 13:03:53) afin d'accélérer le traitement.Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la version plus récente de la page.
Document créé le 11/06/2005, dernière modification le 18/08/2025
Source du document imprimé : https://www.gaudry.be/java-api-javaee-rf-javax/ws/rs/core/NewCookie.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.