Hi Mike and William,

Thanks very much.  Williams second approach worked for me:

request.getCookies().add(new Cookie("PreferredLanguage","en-us"));

That makes a new Cookie object as Mike suggested.

The problem that I had highlights a common problem for me with the Restlet API: It doesn't follow the "standard" structure for Java APIs where getters and setters are used for data. It is rather unintuitive (at least for a Java coder) to use a getter to access a data structure that is modifiable in place. It would have saved me quite a few hours when working with Restlet if the syntax were more like:

Cookie cookie = new Cookie("PreferredLanguage","en-us"));
request.setCookie(cookie);

Regards,
Dave



On Jan 28, 2008, at 5:37 PM, Mike Brzozowski wrote:

William Pietri <william <at> scissor.com> writes:
   request.getCookies().add(new Cookie("PreferredLanguage","en-us"));

Hi David and William,
I think you might need to try a slightly different method. What works for me is
creating a CookieSetting object, like this:

final CookieSetting cookie = new CookieSetting(COOKIE_NAME, cookieContents);
if (rememberMe)
  cookie.setMaxAge(COOKIE_LIFETIME);
response.getCookieSettings().add(cookie);

This hands the cookie back to the client. On subsequent requests, the client will (should?) send along any cookies with the same domain name as your server's address, until they expire. Note that by default cookies expire when the browser
closes; to change this you need to call setMaxAge() as above.

As I understand it, class Cookie is used only for cookies on the client, while CookieSetting is used for _setting_ cookies by the server. This confused me at
first too.

Hope this helps...
--Mike


Reply via email to