javaserverfaces
  1. javaserverfaces
  2. JAVASERVERFACES-2215

java.lang.IllegalStateException: Cannot create a session after the response has been committed

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: 2.1.3
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:

      Mojarra 2.1.3, Tomcat 7.0.19, Glassfish 3.1.1, Eclipse Helios SR2, Win7 x64.

      Description

      When a large page referencing view/session scoped beans is been requested for the first time in a session and its response exceeds the default response buffer size, then the following exception occurs:

      Caused by: java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed
      	at org.apache.catalina.connector.Request.doGetSession(Request.java:2880)
      	at org.apache.catalina.connector.Request.getSession(Request.java:2577)
      	at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:920)
      	at com.sun.faces.context.ExternalContextImpl.getSession(ExternalContextImpl.java:155)
      	at com.sun.faces.renderkit.ServerSideStateHelper.writeState(ServerSideStateHelper.java:175)
      	at com.sun.faces.renderkit.ResponseStateManagerImpl.writeState(ResponseStateManagerImpl.java:122)
      	at com.sun.faces.application.StateManagerImpl.writeState(StateManagerImpl.java:166)
      	at com.sun.faces.application.view.WriteBehindStateWriter.flushToWriter(WriteBehindStateWriter.java:225)
      	at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:418)
      	at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
      	at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
      	at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
      	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      	... 27 more
      

      Here's a testcase.

      View:

      <!DOCTYPE html>
      <html lang="en"
          xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html" 
      >
          <h:head>
              <title>PWC3999 test</title>
          </h:head>
          <h:body>
              <h:form>
                  <h:outputText value="#{bean.exceedBuffer}" />
              </h:form>
          </h:body>
      </html>
      

      Bean:

      @ManagedBean
      @ViewScoped // or @SessionScoped
      public class Bean implements Serializable {
      
          public String getExceedBuffer() {
              int size = FacesContext.getCurrentInstance().getExternalContext().getResponseBufferSize();
              char[] chars = new char[size];
              Arrays.fill(chars, 'x');
              return new String(chars);
          }
      
      }
      

      Fix: create session before the response is committed.

        Issue Links

          Activity

          Show
          ranophoenix added a comment - Workaround: http://forum.primefaces.org/viewtopic.php?f=3&t=13151#p43963
          Hide
          ova2 added a comment -

          Important issue, we need a fix as soon as possible. The workaround is nice but if you have 100+ pages, it's not quite nice to change them and then change again if this bug got fixed.

          Show
          ova2 added a comment - Important issue, we need a fix as soon as possible. The workaround is nice but if you have 100+ pages, it's not quite nice to change them and then change again if this bug got fixed.
          Hide
          balusc added a comment - - edited

          A single filter which is mapped on facesServlet and does the following in doFilter() method is more than sufficient:

          ((HttpServletRequest) request).getSession();
          chain.doFilter(request, response);
          

          It should however not be bugfixed that way in Mojarra side. Sometimes there is the desire to keep JSF pages entirely stateless, so creation of session should only be done on demand (i.e. when there's a need for a view or session scoped bean).

          Show
          balusc added a comment - - edited A single filter which is mapped on facesServlet and does the following in doFilter() method is more than sufficient: ((HttpServletRequest) request).getSession(); chain.doFilter(request, response); It should however not be bugfixed that way in Mojarra side. Sometimes there is the desire to keep JSF pages entirely stateless, so creation of session should only be done on demand (i.e. when there's a need for a view or session scoped bean).
          Hide
          Balázs Zsoldos added a comment -

          I found a workaround (at least in my case it works):

          <context-param>
          <param-name>com.sun.faces.writeStateAtFormEnd</param-name>
          <param-value>false</param-value>
          </context-param>

          In this case the state is written in the encodeBegin function of the form so the response is not commited for sure.

          Show
          Balázs Zsoldos added a comment - I found a workaround (at least in my case it works): <context-param> <param-name>com.sun.faces.writeStateAtFormEnd</param-name> <param-value>false</param-value> </context-param> In this case the state is written in the encodeBegin function of the form so the response is not commited for sure.
          Hide
          datakey added a comment -

          Another case is when session is invalidated and not "forced creation" again.

          Show
          datakey added a comment - Another case is when session is invalidated and not "forced creation" again.
          Hide
          Manfred Riem added a comment -

          Closing this as a duplicate, progress will be tracked at the other issue

          Show
          Manfred Riem added a comment - Closing this as a duplicate, progress will be tracked at the other issue

            People

            • Assignee:
              rogerk
              Reporter:
              balusc
            • Votes:
              11 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: