0
votes

I've developed web apps using JSF 2.0 and now I decided to write app using Myfaces 2.2 and Tomcat 7.0.35. But I get exceptions and can't understand the reason of this. I khow that stacktrace very similar to Mojara bug but I use Myfaces.

java.lang.IllegalStateException: Cannot create a session after the response has been committed
    org.apache.catalina.connector.Request.doGetSession(Request.java:2881)
    org.apache.catalina.connector.Request.getSession(Request.java:2316)
    org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:898)
    org.apache.myfaces.context.servlet.ServletExternalContextImpl.getSession(ServletExternalContextImpl.java:150)
    org.apache.myfaces.view.impl.DefaultViewScopeHandler.generateViewScopeId(DefaultViewScopeHandler.java:128)
    org.apache.myfaces.view.ViewScopeProxyMap.getWrapped(ViewScopeProxyMap.java:76)
    org.apache.myfaces.view.ViewScopeProxyMap.isEmpty(ViewScopeProxyMap.java:94)
    org.apache.myfaces.renderkit.ErrorPageWriter._writeVariables(ErrorPageWriter.java:787)
    org.apache.myfaces.renderkit.ErrorPageWriter._writeVariables(ErrorPageWriter.java:769)
    org.apache.myfaces.renderkit.ErrorPageWriter.debugHtml(ErrorPageWriter.java:356)
    org.apache.myfaces.renderkit.ErrorPageWriter.handle(ErrorPageWriter.java:470)
    org.apache.myfaces.context.MyFacesExceptionHandlerWrapperImpl.handle(MyFacesExceptionHandlerWrapperImpl.java:301)
    javax.faces.context.ExceptionHandlerWrapper.handle(ExceptionHandlerWrapper.java:61)
    org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:287)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:200)

It's my page

<h:body>
    <h:inputText value="#{loginBean.userName}"/>
    <h:inputSecret value="#{loginBean.password}"/>
    <h:commandButton value="Login"/>    
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <h:outputScript name="lib/bootstrap.min.js" library="js" target="body"/>
</h:body>

@ManagedBean(name = "loginBean")
@RequestScoped
public class LoginBean implements Serializable{

    private String userName;
    private String password;

    @PostConstruct
    public void init(){
        userName = "";
        password = "";
    }

    public void validateCredentials(){

    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

What can be the reason of this problem?

2
It looks like there is a bug, and an exception is thrown, but the session is created time after the headers are sent, so the session cannot be created and another exception is thrown. One option is create a phase listener and before render response phase call force session creation, to see the other underlying exception.lu4242

2 Answers

1
votes

Try to add:

<h:form rendered="#{loginBean.initOk}">
   <h:inputText value="#{loginBean.userName}"/>
   <h:inputSecret value="#{loginBean.password}"/>
   <h:commandButton value="Login"/>   
   ............


<h:from>

@ManagedBean(name = "loginBean")
@RequestScoped
public class LoginBean implements Serializable{

 private String userName;
 private String password;
 private boolean initOk;


@PostConstruct
public void init(){
    userName = "";
    password = "";
    initOk =true;
}

all getter setter...
0
votes

I have the same problem with my ConversationScoped bean. I´m working with jboss AS 7.1 and jsf 2.

The following is my stacktrace:

15:38:01,911 Information [javax.enterprise.resource.webcontainer.jsf.context] (http-localhost-127.0.0.1-8080-1) Exception when handling error trying to reset the response.: java.lang.IllegalStateException: Cannot create a session after the response has been committed
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2636) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.Request.getSession(Request.java:2375) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:841) [jbossweb-7.0.13.Final.jar:]
    at    

then I add the next method:

@PostConstruct
public void postConstruct() {
    FacesContext.getCurrentInstance().getExternalContext().getSession(true);

}

and the issue is no more.