4/23/2011

How to read and write cookies in ICEfaces 2.0

Just a quick note before I leave to my easter vacation. If you try to get and set cookies in ICEfaces 2.0 like you did in 1.82 you will likely fail to do so. The 1.82 way was:



Map cookies = ((HttpServletRequest)((BridgeExternalContext) FacesContext.getCurrentInstance().getExternalContext()).getRequest()).getCookies();

Not very intuitive, if you ask me. I clearly prefer the 2.0 - way, which is the official JSF 2.0 way:

//Setting the cookie

FacesContext.getCurrentInstance().getExternalContext()
.addResponseCookie("CookieName", "CookieValue", null);

//Getting the cookie

Map<String, Object> requestCookieMap = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap();



If you need to add more properties you can do so by providing a properties map when calling addResponseCookie. Which properties you can add to the map is described here:


Official Oracle JavaDoc


If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.