title: Understanding HttpServletRequest and cookies in JSF
body: <p>In order to create a "Remember me" login in JSF, I am trying to understand how Cookies work. I have created a brand new Web Application using JSF, with this bean that creates a Cookie expiring with the session:</p>
  
  <p><strong>CookieBean</strong> class</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class CookieBean implements Serializable {
      public void create() {
          ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
          ec.addResponseCookie("MyTestCookie", "Hello Cookie", null);
      }    
  }
  </code></pre>
  
  <p>and <strong>index.xhtml</strong> has this body:</p>
  
  <pre><code>&lt;h:form&gt;
      &lt;h:commandButton value="Create Cookie!" action="#{cookieBean.create()}" &gt;
          &lt;f:ajax render="@form" /&gt;
      &lt;/h:commandButton&gt;
      &lt;p&gt;&lt;/p&gt;
      &lt;h:outputText value="Cookie value: #{cookie['MyTestCookie'].value}" /&gt; 
  &lt;/h:form&gt;
  </code></pre>
  
  <p>As a result, when the page first loads, there is no cookie, correctly, because it's the first time the application runs, and no cookie is there.</p>
  
  <p>After clicking the button once, no cookie is displayed. Why? The button invokes the <code>cookieBean#create()</code> method, and the ajax tag should force a revaluation of the <code>outputText</code> component. And this should generate an <code>HttpSerlvetRequest</code> with the cookie... or not? The cookie shows only after I press the button again!. </p>
  
  <p>More surprisingly, when I press the <code>refresh</code> button of the browser, the cookie is not shown, although I'd expect to see it, because the older session is still alive.</p>
  
  <p>It's like if (re)loading the page doesn't send an HttpServletRequest to the server... </p>