My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

vineri, 19 iunie 2015

[OmniFaces utilities (2.1)] Add a cookie with given name, value, path, domain and maxage to the HTTP response


Because Google Chrome doesn't like domain: "localhost" on cookies, OmniFaces adapted this method in version 2.1 to skip setting cookie domain when it equals "localhost". This is still available in vesion 2.0.

[OmniFaces utilities] The addResponseCookie() method add a cookie with given name, value, path and maxage to the HTTP response. The cookie value will implicitly be URL-encoded with UTF-8 so that any special characters can be stored in the cookie. The cookie will implicitly be set to secure when the current request is secure (i.e. when the current request is a HTTPS request). The cookie will implicitly be set in the domain and path of the current request URL.

Note I: The maxAge argument represents the maximum age of the cookie, in seconds. If this is 0, then the cookie will be removed. Note that the name and path must be exactly the same as it was when the cookie was created. If this is -1 then the cookie will become a session cookie and thus live as long as the established HTTP session.
Note II: The path argument represents the cookie path. If this is /, then the cookie is available in all pages of the webapp. If this is /somespecificpath, then the cookie is only available in pages under the specified path.
Note III: The domain argument represents cookie domain. You can use .example.com (with a leading period) if you'd like the cookie to be available to all subdomains of the domain. Note that you cannot set it to a different domain.

Method:
Usage:

·         inside a servlet filter or even a plain vanilla servlet (basically, when FacesContext is not available):

import org.omnifaces.util.Servlets;
...
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
 protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // add here the cookies as in the below examples
 }
 ...
}

·         in JSF, when FacesContext is available use, Faces#addResponseCookie(), or more clumsy (not recommended):

import org.omnifaces.util.Servlets;
...
// or simply use, Faces#getRequest() and Faces#getResponse()
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext externalContext = context.getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
// add here the cookies as in the below examples

Examples:

Add a cookie with given name, value and maxage to the HTTP response.

// cookie will be removed
Servlets.addResponseCookie(request, response, "fooCookie", "foo", 0);

// cookie will expire after 15 seconds
Servlets.addResponseCookie(request, response, "fooCookie", "foo", 15);

// session cookie
Servlets.addResponseCookie(request, response, "fooCookie", "foo", -1);


Add a cookie with given name, value, path and maxage to the HTTP response.

// session cookie with explicit path
Servlets.addResponseCookie(request, response, "fooCookie", "foo", "/mypath", -1);


Add a cookie with given name, value, domain, path and maxage to the HTTP response.

// session cookie with explicit path and domain
Servlets.addResponseCookie(request, response, "fooCookie", "foo", ".mydomain.com", "/mypath", -1);


// setting 'localhost' as domain is taken into account in OmniFaces 2.0, but not in 2.1
Servlets.addResponseCookie(request, response, "fooCookie", "foo", "localhost", "/mypath", -1);

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors