Java Reference
In-Depth Information
And here is the output to the browser, as you would expect:
The value in your cookie was: MY_VALUE
You can also use other methods on the NewCookie , as well as additional arguments to its con-
structor, to do things like set an expire time, set a domain and path name, or set a version
number.
NOTE
If you're using the Firefox browser, a terrific plug-in for debugging HTTP applications is Live HTTP
Headers, which shows you the headers being sent with requests and responses.
Using HttpHeaders to access cookies
You can also get a Map<String, Cookie> that contains all cookies in the request by using the
getCookies method off of HttpHeaders . The string type parameter on the map is the key to
the cookie value. You can then iterate the map using a key set:
//retrieve cookie using injected HttpHeaders
@GET
@Path("/step3")
public String step3(@Context HttpHeaders headers) {
System.out.println("Step 3.");
String html = "";
if (headers != null) {
html = "<html><body>Headers:<br/>";
final Map<String, Cookie> cookies = headers.getCookies();
for (String key : cookies.keySet()) {
html += "Cookie " + key + "=" +
cookies.get(key) + "<br>";
}
html += "</body></html>";
} else {
html = "<html><body>Could not find cookies. :(</body></html>";
}
return html;
}
Search WWH ::




Custom Search