Java Reference
In-Depth Information
public
public class
class
Cookie
Cookie
{
public
public
String
getName
() {...}
public
public
String
getValue
() {...}
public
public
int
int
getVersion
() {...}
public
public
String
getDomain
() {...}
public
public
String
getPath
() {...}
...
}
The
getName()
and
getValue()
methods correspond to the string name and value of the
cookie you are injecting. The
getVersion()
method defines the format of the cookie head-
main()
method specifies the DNS name that the cookie matched. The
getPath()
method
corresponds to the URI path that was used to match the cookie to the incoming request. All
these attributes are defined in detail by the IETF cookie specification.
You can also obtain a map of all cookies sent by the client by injecting a reference to
javax.ws.rs.core.HttpHeaders
:
public
public interface
interface
HttpHeaders
HttpHeaders
{
...
public
public
Map
<
String
,
Cookie
>
getCookies
();
}
As you saw in the previous section, you use the
@Context
annotation to get access to
Ht-
tpHeaders
. Here's an example of logging all cookies sent by the client:
@Path
(
"/myservice"
)
public
public class
class
MyService
MyService
{
@GET
@Produces
(
"text/html"
)
public
public
String
get
(
@Context
HttpHeaders headers
) {
for
for
(
String name
:
headers
.
getCookies
().
keySet
())
{
Cookie cookie
=
headers
.
getCookies
().
get
(
name
);
System
.
out
.
println
(
"Cookie: "
+
name
+
"="
+
cookie
.
getValue
());
}
...
}
}