7 Replies Latest reply on Jan 7, 2010 9:46 AM by jbalunas

    Cookie in Ajax Response deleted.. ?

    rosi75

      Hi

      I'm trying for many hours to solve this problem. I found no docu about that.

      I just try to add some cookies in a AJAX Respond. But it does not work. I also tried to put some headers into the response ("set-cookie...") but they will also be resetted during ajax response update..

      Is it impossible to set any cookie by ajax-respond?

      many thanks for any help.
      rgds
      rosi

        • 1. Re: Cookie in Ajax Response deleted.. ?
          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

            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

              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

                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

                  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

                    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

                      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