I try to read cookies from my Session Bean MainpageManager
The strange thing is that my Bean is initialized twice. So first the cookies are found and then they are not included in the Map.
I try Reading and Writing Cookies like this:
Reading
Code:
Map<String, Object> requestCookieMap = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap(); System.out.println("Cookies: "+requestCookieMap.keySet()); Cookie cookieUserCookie = (Cookie) requestCookieMap.get("user"); Cookie cookiePasswordCookie = (Cookie) requestCookieMap.get("password"); if (cookieUserCookie != null) { String cookieUser = cookieUserCookie.getValue(); if (cookieUser != null) { dataStorage.setCookieUser(cookieUser); System.out.println("cookieUser: "+cookieUser); } } if (cookiePasswordCookie != null) { String cookiePassword = cookiePasswordCookie.getValue(); if (cookiePassword != null) dataStorage.setCookiePassword(cookiePassword); System.out.println("cookiePassword: "+cookiePassword); }
Writing:
Code:
FacesContext.getCurrentInstance().getExternalContext().addResponseCookie("user", dataStorage.getCookieUser(), null); FacesContext.getCurrentInstance().getExternalContext().addResponseCookie("password", dataStorage.getCookiePassword(), null);
The Cookies are written but my App doesn't find them any more.
The Debug Output is:
Code:
DataStorage is created Start init Cookies: [user, password, JSESSIONID, ice.push.browser, ice.connection.lease, ice.connection.contextpath, ice.connection.running] cookieUser: username cookiePassword: password New User created Finished init getUser(): username DataStorage is created Start init Cookies: [JSESSIONID, ice.push.browser, ice.connection.lease, ice.connection.contextpath, ice.connection.running] New User created Finished init getUser():
What is done wrong by me when reading and writing cookies?
Why is the Session Bean Class initialized twice?
Why does it find the cookies the first time but not the second time?
![]() ![]() |
![](https://i-blog.csdnimg.cn/blog_migrate/aa33e348960091e422c93012e1d81f9e.png)
Joined: 10/Jun/2008 00:00:00
Messages: 20
Online
Solved the problem:
a) I didn't set the maxAge property so the cookie was only available in the current session.
b) After this mistake I didn't set all properties, they are required
additional Information:
1) path in the jsf api means the path after the ip/domain and port
i.e.: testdomain.com:8080/testpath/ -> path = "/testpath/"
2) according to the jsf api property comment is required, but then I got an error, is working without
Example:
Code:
Hashtable<String,Object> properties = new Hashtable<String,Object>(); //properties.put("comment", "comment"); properties.put("domain", "127.0.1"); properties.put("maxAge", 31536000); properties.put("secure", false); properties.put("path","/webapp/"); FacesContext.getCurrentInstance().getExternalContext().addResponseCookie("user", dataStorage.getCookieUser(), properties); FacesContext.getCurrentInstance().getExternalContext().addResponseCookie("password", dataStorage.getCookiePassword(), properties);
![]() ![]() |