Hi,

 

Kindly advise/help how to create/retrieve a cookie in a java managed bean that will work in Safari browser.

Below are the implementations I used that is working completely fine in other browsers (such as Chrome).

 

For creating cookies:

     HttpServletResponse rep = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();

     Cookie myCookie = new Cookie(cookieName, cookieValue);

     myCookie.setSecure(Boolean.valueOf(false));

     myCookie.setMaxAge(-1);

     myCookie.setPath(<URL string>);

     rep.addCookie(myCookie);

 

For retrieving cookies:

     HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

     Cookie[] cookies = req.getCookies();

     for (int i = 0; i < cookies.length; i++) {

          name = cookies[i].getName();

          value = cookies[i].getValue();

     }

 

Using the above code, req.getCookies() ONLY returns "JSESSIONID" without the other cookies I've added.

 

Thank you in advance for any help anyone can give me.

 

Regards,

Rhey

 

P.S. Why is Safari eating the cookies?

FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners