-
1. Re: Cookie in Ajax Response deleted.. ?
rosi75 Jun 23, 2008 10:48 AM (in response to rosi75)perhaps some more exact information about that:
jsp:<f:view locale="#{wopiBean.locale}"> <h:form id="wopidruck" onclick="tt_HideInit();" onkeydown="tt_HideInit();"> <a4j:region id="berater_region" renderRegionOnly="true" > <h:inputText styleClass="input input_FeldVOLL" value="#{wopiBean.mapper['NACHNAME'].value}" id="nachn"> <a4j:support id="nachn_a4j" event="onchange" ajaxSingle="true" eventsQueue="person" /> </h:inputText> </a4j:region> </h:form>
This section is just one inputfield with a value. This values should be sent as a cookie to the client - on every field change !
But i does not work. The action method which I call in h:inputText does the following:... HttpServletResponse response = (HttpServletResponse) JsfHelper.getFacesContext().getExternalContext().getResponse(); Cookie cookie = new Cookie(Konstants.COOKIE_SIRNAME,sirname) ); cookie2.setMaxAge( Konstants.COOKIE_TIMETOLIVE ); response.addCookie( cookie ); ...
at the end I get an ajax-response without set-cookie header... -
2. Re: Cookie in Ajax Response deleted.. ?
rosi75 Jun 26, 2008 7:18 AM (in response to rosi75)I think Richfaces isn't prepared for sending cookies in responses.
Can somebody who knows about confirm this? -
3. Re: Cookie in Ajax Response deleted.. ?
alexsmirnov Jun 26, 2008 5:40 PM (in response to rosi75)This is a working code from my sample:
public String setCookie() { ExternalContext externalContext = FacesContext.getCurrentInstance() .getExternalContext(); HttpServletResponse response = (HttpServletResponse) externalContext .getResponse(); Cookie cookie = new Cookie("test", "Setted at time " + System.currentTimeMillis()); cookie.setMaxAge(60 * 60 * 24 * 365); response.addCookie(cookie); return "verify_cookie"; } public String getTestCookie() { ExternalContext externalContext = FacesContext.getCurrentInstance() .getExternalContext(); HttpServletRequest request = (HttpServletRequest) externalContext .getRequest(); Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) { if ("test".equals(cookies.getName())) { return cookies.getValue(); } } return null; }
This methods used on a page:<a4j:form> <h:outputText value="Cookie value: #{bean.testCookie}"/> <a4j:commandButton action="#{bean.setCookie}" value="Set Cookie by AJAX"/> <h:commandButton action="#{bean.setCookie}" value="Set Cookie in POST"/> </a4j:form>
-
4. Re: Cookie in Ajax Response deleted.. ?
rosi75 Jul 9, 2008 11:00 AM (in response to rosi75)hey thanks for your reply.
I tried to do so. I saw that I am using h:form instead of a4j:form.. but also this didn't solve my problem.
so, perhaps my system is already a bit complicate for such easy solution. perhaps I will try your solution in an clean and young environment.
regards. -
5. Re: Cookie in Ajax Response deleted.. ?
artdaw Aug 20, 2008 6:29 AM (in response to rosi75)Hi, rosi75!
You could try this code:public class CookieBean { private String testCookie = null; public String setTestCookie() { ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); Cookie cookie = new Cookie("test", "Setted at time " + System.currentTimeMillis()); cookie.setMaxAge(60 * 60 * 24 * 365); response.addCookie(cookie); return "verify_cookie"; } public String getTestCookie() { ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); HttpServletRequest request = (HttpServletRequest) externalContext.getRequest(); Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) { if (cookies.getName().equals("test")) { testCookie = cookies.getValue(); return testCookie; } } return null; } }
The usage of these methods and testCookie property on a page:<h:form> <h:outputText id="text" value="Cookie value: #{cookieBean.testCookie}"/> <br /> <h:panelGrid columns="2"> <a4j:commandButton action="#{cookieBean.setTestCookie}" value="Set Cookie by AJAX"> <a4j:support event="oncomplete" reRender="text" /> </a4j:commandButton> <h:commandButton action="#{cookieBean.setTestCookie}" value="Set Cookie in POST" reRender="text"/> </h:panelGrid> </h:form>
Actually I've used <h:form> and <a4j:form> equally successful. -
6. Re: Cookie in Ajax Response deleted.. ?
schenger Jan 7, 2010 8:28 AM (in response to alexsmirnov)Hi Alexander,
i get an ClassCastException if i try to set cookies the way you did:
Caused by: java.lang.ClassCastException: org.jboss.portletbridge.richfaces.BufferedResourceResponseWrapper cannot be cast to javax.servlet.http.HttpServletResponse
I am working on Jboss Portal 2.7.2 with PortletBridge 2.0.0.BETA and Richfaces 3.3.2.maybe you have a porposition what is wrong?
thanks a lot
-
7. Re: Cookie in Ajax Response deleted.. ?
jbalunas Jan 7, 2010 9:46 AM (in response to schenger)Hi
This forum is in the archive area and is not used. I'll try to move this thread to the RichFaces area.
Thanks,
Jay