4 Replies Latest reply on Apr 4, 2007 10:29 PM by rhinox

    HttpServletResponse.addCookie(Cookie) is not working when us

    rhinox

      When I use any of the a4j components to generate an AJAX request (e.g a4j:commandButton instead of h:commandButton) the method HttpServletResponse.addCookie(Cookie) is not working. I have the following test case:

      Bean
      TestCookieBean.java

      public class TestCookieBean {
       public String test() {
       ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
       HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
       Cookie cookie = new Cookie("test", "123456");
       cookie.setMaxAge(60*60*24*365);
       response.addCookie(cookie);
       return null;
       }
      }
      


      View
      index.jsp
      <%@page contentType="text/html"%>
      <%@page pageEncoding="UTF-8"%>
      <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
      <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
      <%@taglib uri="https://ajax4jsf.dev.java.net/ajax" prefix="a4j" %>
      <html>
       <head></head>
       <body>
       <f:view>
       <a4j:form>
       <a4j:commandButton action="#{testCookieBean.test}" value="Test Cookie" />
       </h:form>
       </f:view>
       </body>
      </html>
      


      If I change the <a4j:commandButton> for <h:commandButton> the cookie gets created.

      Please help!

      Thanks!

        • 1. Re: HttpServletResponse.addCookie(Cookie) is not working whe
          ilya_shaikovsky

          Sorry, I've just copied your method and include the same Ajax button to my page and all works - as expected. So describe your environment more clearly.

          I've used:
          JSF 1.2.04
          Facelets 1.1.12
          Ajax4jsf and Rich Faces - latest SNAPSHOTS.

          • 2. Re: HttpServletResponse.addCookie(Cookie) is not working whe
            rhinox

            I've found the problem, if navigation rules of the faces-config.xml are declared as redirect the cookie is not created, but I need the redirect. I have the following:

            JSF RI 1.2_05
            AJAX4JSF 1.1.1-20070402.230943-21 (SNAPSHOT)
            Tomahawk 1.1.5 (SNAPSHOT)
            Facelets 1.1.11
            Sun App Server 9.1 Beta

            Here is the code:

            Bean
            TestCookieBean.java

            public class TestCookieBean {
             public String test() {
             ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
             HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
             Cookie cookie = new Cookie("test", "123456");
             cookie.setMaxAge(60 * 60 * 24 * 365);
             response.addCookie(cookie);
             return "verify_cookie";
             }
            }
            


            Views
            TestCookie.xhtml
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml"
             xmlns:ui="http://java.sun.com/jsf/facelets"
             xmlns:h="http://java.sun.com/jsf/html"
             xmlns:f="http://java.sun.com/jsf/core"
             xmlns:t="http://myfaces.apache.org/tomahawk"
             xmlns:a4j="https://ajax4jsf.dev.java.net/ajax">
            <f:view>
             <t:document>
             <t:documentHead>
             <meta http-equiv="content-type" content="text/xhtml; charset=utf-8; pageEncoding=utf-8"/>
             <meta http-equiv="pragma" content="no-cache"/>
             <meta http-equiv="cache-control" content="no-cache"/>
             <meta http-equiv="expires" content="0"/>
             <title>Test Cookie</title>
             </t:documentHead>
             <t:documentBody>
             <a4j:form>
             <a4j:commandButton action="#{testCookieBean.test}" value="Test Cookie"/>
             </a4j:form>
             </t:documentBody>
             </t:document>
            </f:view>
            </html>
            


            VerifyCookie.xhtml
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml"
             xmlns:ui="http://java.sun.com/jsf/facelets"
             xmlns:h="http://java.sun.com/jsf/html"
             xmlns:f="http://java.sun.com/jsf/core"
             xmlns:t="http://myfaces.apache.org/tomahawk"
             xmlns:a4j="https://ajax4jsf.dev.java.net/ajax">
            <f:view>
             <t:document>
             <t:documentHead>
             <meta http-equiv="content-type" content="text/xhtml; charset=utf-8; pageEncoding=utf-8"/>
             <meta http-equiv="pragma" content="no-cache"/>
             <meta http-equiv="cache-control" content="no-cache"/>
             <meta http-equiv="expires" content="0"/>
             <title>Verify Cookie</title>
             </t:documentHead>
             <t:documentBody>
             <h:outputText value="Cookie value: #{cookie['test']}"/>
             </t:documentBody>
             </t:document>
            </f:view>
            </html>
            


            faces-config.xml
            <?xml version='1.0' encoding='UTF-8'?>
            <faces-config xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
             version="1.2">
             <managed-bean>
             <managed-bean-name>testCookieBean</managed-bean-name>
             <managed-bean-class>com.test.jsf.TestCookieBean</managed-bean-class>
             <managed-bean-scope>session</managed-bean-scope>
             </managed-bean>
             <navigation-rule>
             <navigation-case>
             <from-outcome>verify_cookie</from-outcome>
             <to-view-id>/verifyCookie.xhtml</to-view-id>
             <redirect/>
             </navigation-case>
             </navigation-rule>
            </faces-config>
            


            Try adding/removing the redirect from the navigation rule to reproduce the issue.

            Thanks!

            • 3. Re: HttpServletResponse.addCookie(Cookie) is not working whe

              we will investigate this issue.
              http://jira.jboss.com/jira/browse/AJSF-31 has been added to jira.

              • 4. Re: HttpServletResponse.addCookie(Cookie) is not working whe
                rhinox

                I've attached a WAR file at JIRA to reproduce the problem.

                Thanks.