Apache Wicket > Framework Documentation > Reference library > How to do things in Wicket > View Layer > Dealing with cookies
Added by Loren Rosen, last edited by Loren Rosen on Jan 09, 2007

To send and retrieve cookies, use the WebResponse and WebRequest objects, respectively. These in turn can be obtained from the RequestCycle, which can be retrieved by calling the getRequestCycle method on a Component. Thus to create a cookie:

        ((WebResponse)getRequestCycle().getResponse()).addCookie(new Cookie("cookieName", "cookieValue"));

and to retrieve all cookies:

        Cookie[] cookies = ((WebRequest)getRequestCycle().getRequest()).getCookies();

Alternatively, anywhere on the thread that is responding to a web request:

        ((WebResponse)RequestCycle.get().getResponse()).addCookie(new Cookie("cookieName", "cookieValue"));

and similarly for retrieving cookies.

Finally, the casts can be avoided if you are within a subclass of WebPage:

        getWebRequestCycle().getWebResponse().addCookie(new Cookie("cookieName", "cookieValue"));

and similarly for retrieving cookies.