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".
[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
// 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);







JSF 2 Tutorials at www.mkyong.com
JavaServer Faces (JSF) Tutorial 

















Niciun comentariu :
Trimiteți un comentariu