Question

I got few doubts on sessions in JSF and/or in general.

First up, does session gets created on load of an web app in AS/servlet container? Meaning, after I do right click- run as - run on server and the app is started and synchornised?

Or does it get created after the web app is loaded and the defined welcome page is displayed and then the client makes a request?

Second: I was looking into the issue of "Cannot create a session after the response has been committed" in jsf.

I was trying to reproduce the issue by doing this given in the following site, http://java.net/jira/browse/JAVASERVERFACES-2215

But, I didnt get any error as mentioned above. I tried with all bean scope. Was the session created?

This is the code.

  <h:body>
      <h:form id="test">
      <p:growl id="msg"></p:growl>
        <h:outputText value="#{myTestBean.exceedBuffer}" />

      </h:form>
   </h:body>

bean code is.

   public String getExceedBuffer() {    
       int size = FacesContext.getCurrentInstance().getExternalContext().getResponseBufferSize();
      char[] chars = new char[size];
       Arrays.fill(chars, 'x');
       return new String(chars);
    }

My understanding is the following,

First when the app is loaded the servlet container or an AS creates a session and a sessionID(JSESSIONID) and puts it in server memory. So the initial page gets loaded up anyhow, meaning even if the response buffer is overflowed or not but the session wont be created. Now, when the client makes a request on this page, since the session was not created, the error occurs. But, I tried even doing this by

<p:commandButton actionListener="#{myTestBean.onSelect}" update=":test"></p:commandButton>   

and in onSelect method just entered a dummy code. Even now the page displayed as usual.

Well, that was when I was completely lost. Can someone pls help me on this? Thanks in advance.

Was it helpful?

Solution

First up, does session gets created on load of an web app in AS/servlet container? Meaning, after I do right click- run as - run on server and the app is started and synchornised?

Or does it get created after the web app is loaded and the defined welcome page is displayed and then the client makes a request?

It's created on start of the HTTP session, not on webapp's startup (that's the application scope). Basically, when request.getSession() is called for the first time and the HttpSession hasn't been created yet. This all is handled by the Servlet API which JSF is using under the covers. So the following answer should help in understanding how it works: How do servlets work? Instantiation, sessions, shared variables and multithreading


Second: I was looking into the issue of "Cannot create a session after the response has been committed" in jsf.

I was trying to reproduce the issue by doing this given in the following site, http://java.net/jira/browse/JAVASERVERFACES-2215

But, I didnt get any error as mentioned above. I tried with all bean scope. Was the session created?

You will get this error when the HttpSession isn't created yet at the moment the response is committed. That you didn't get this error can only mean that the session has already been created beforehand. You need to test it on a freshly started webapp and the session shouldn't be created yet. Note that some servers by default stores sessions on shutown/restart. You'd need to disable this or to remove the JSESSIONID cookie in your browser before sending the first request on this test page. Restarting the webbrowser or using a different one should also force a fresh new session.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top