Java Reference
In-Depth Information
level path mapping, and then the additional GET methods respond to the same path appended
with their annotation's values.
To create the cookie you use the NewCookie class, which will also accept a Cookie instance as
a constructor argument. Then write some HTML that you'll send as your response, and pass
them both into the response builder chain. The ok factory method creates a 200 OK response
template; set your cookie on it and call build to prepare the final response.
Here are the HTTP headers in the response you get from the service after accessing the home
page:
HTTP/1.x 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: MY_KEY=MY_VALUE;Version=1
Content-Type: text/html
Transfer-Encoding: chunked
Date: Wed, 05 Nov 2008 21:05:42 GMT
As you can see from this response, the cookie has been set. Clicking the link on the returned
HTML will then pass the newly set cookie along with the request, allowing you to read the
value back on the server. The request containing the cookie looks like this:
http://localhost:8080/restMaven/resources/cookie/step2
GET /restMaven/resources/cookie/step2 HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.7)
Gecko/20061011 Fedora/1.5.0.7-7.fc6 Firefox/1.5.0.7
Accept: text/xml,application/xml,application/xhtml+xml,text/html;
q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost:8080/restMaven/resources/cookie/home
Cookie: MY_KEY=MY_VALUE
The standard Cookie header contains the cookie data just as you set it.
Here is the total output in the server log:
Home page.
Built response.
Step 2.
Cookie Name: MY_KEY
Cookie Value: MY_VALUE
Sending response for step2.
Search WWH ::




Custom Search