A cookie is a map of the cookie values of an instance request . Therefore, you cannot use it because “setting a cookie” means “adding a cookie to the response”.
So, as follows from the article, you should use the response object.
var response = facesContext.getExternalContext().getResponse();
var userCookie = new javax.servlet.http.Cookie("name", "value");
userCookie.setMaxAge(60*60*24*365*10);
userCookie.setPath("/");
response.addCookie(userCookie);
source
share