title: When does JSF creates a session & what does it puts in a session map?
body: <p>I am running Mojarra 2.2.0.  </p>
  
  <pre><code>  &lt;context-param&gt;
      &lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt;
      &lt;param-value&gt;client&lt;/param-value&gt;
    &lt;/context-param&gt;
  </code></pre>
  
  <p>The managed bean action method is-</p>
  
  <pre><code>public void action() {
          HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
                  .getExternalContext().getSession(false);
          System.out.println(session.getId()); // not null for stateful views
      }
  </code></pre>
  
  <p>For <code>stateless</code> views <code>session.getId()</code> throws <code>NPE</code></p>
  
  <p>For views which are not stateless-
  Firing a GET request, there is <code>JSESSIONID=340041C96D5AA446D761C3602F54A76D</code></p>
  
  <p>I read it <a href="https://stackoverflow.com/questions/4095713/no-session-generation-in-jsf/4096992?noredirect=1#comment60890865_4096992">here</a> that-</p>
  
  <blockquote>
    <p>For client side state saving mechanism,  JSF won't create the session
    and will store the view state in a hidden input field with the name
    javax.faces.ViewState in the form whenever necessary.</p>
  </blockquote>
  
  <p>Further, it's mentioned here <a href="https://stackoverflow.com/questions/14834016/when-is-session-created-during-jsf-login">that</a></p>
  
  <blockquote>
    <p>JSF will indeed autocreate the session because the JSF view state has
    to be stored over there. If you set the JSF state saving method to
    client instead of server, then it won't be stored in session and hence
    no session needs to be created</p>
  </blockquote>
  
  <p>I think the above line is a source for trouble for me.</p>
  
  <blockquote>
    <p>If you set the JSF state saving method to client instead of server,
    then it won't be stored in session // FULLY AGREED</p>
  </blockquote>
  
  <p>and </p>
  
  <blockquote>
    <p>hence no session needs to be created. // This confuses because for
    client side saving mechanism, a session id gets generated by the
    servlet container &amp; hence there is a session associated with the
    request.</p>
  </blockquote>
  
  <p>In reference to the discussion which I had with BalusC in this <a href="https://stackoverflow.com/questions/4095713/no-session-generation-in-jsf/4096992?noredirect=1#comment60890865_4096992">question</a>,
  I created a HttpSessionListener-</p>
  
  <pre><code>@WebListener
  public class MyHttpSessionListener implements HttpSessionListener {
  
      public void sessionCreated(HttpSessionEvent event) {
          Thread.dumpStack();
      }
  
      public void sessionDestroyed(HttpSessionEvent event) {
  
      }
  
  }
  </code></pre>
  
  <p>See below attached screenshots(these 2 screenshots are for version 2.0.3, there must have been an old bug due to which the session was getting created)-</p>
  
  <p><a href="https://i.stack.imgur.com/8tkPN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8tkPN.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/3EgKh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3EgKh.png" alt="enter image description here"></a></p>
  
  <p>Libraby (Mojarra 2.2.0)-
  <a href="https://i.stack.imgur.com/w5TqE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w5TqE.png" alt="enter image description here"></a></p>