Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use cookies in JSF

Tags:

I have a JSF form over a JSF 1.2 Session Scope Bean. I have a "Reset" button which invalidates the session.

I tried to use cookies to remember a session number (Not JSF session but my private session number) between sessions but I failed. My question - Where is the correct place (Some listener? Bean Constructor?) to initialize, retrieve and store the cookie.

Looking for the best method to do this.

Thanks!

like image 668
Ben Avatar asked Apr 05 '11 19:04

Ben


People also ask

How to get cookie in servlet?

Procedure. Use the HTTP request object that is passed to the service method of your servlet to get all cookies that the client browser sent with the request. 1. Use the getCookies() method of the HttpServletRequest to retrieve an array of all cookies in the request.

What is the use of cookies in java?

Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.

What is session and cookies in Java?

Cookies are client-side files on a local computer that hold user information. Sessions are server-side files that contain user data. Cookies end on the lifetime set by the user. When the user quits the browser or logs out of the programmed, the session is over. It can only store a certain amount of info.

What is cookie in servlet?

A cookie is a small piece of information that is persisted between the multiple client requests. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.


1 Answers

You can obtain all cookies by ExternalContext#getRequestCookieMap()

Map<String, Object> cookies = externalContext.getRequestCookieMap(); // ... 

When running JSF on top of Servlet API (which is true in 99.9% of the cases ;) ), the map value resolves to javax.servlet.http.Cookie.

Cookie cookie = (Cookie) cookies.get(name); // ... 

In JSF 1.2, which lacks the JSF 2.0-introduced ExternalContext#addResponseCookie() method, you need to cast the ExternalContext#getResponse() to HttpServletResponse (only when running JSF on top of Servlet API of course) and then use the HttpServletResponse#addCookie().

HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); Cookie cookie = new Cookie(name, value); cookie.setMaxAge(maxAge); // Expire time. -1 = by end of current session, 0 = immediately expire it, otherwise just the lifetime in seconds. response.addCookie(cookie); 

You can do this anywhere in the JSF context you want, the right place depends on the sole functional requirement. You only need to ensure that you don't add the cookie when the response has already been committed, it would otherwise result in an IllegalStateException.

like image 51
BalusC Avatar answered Oct 24 '22 01:10

BalusC