JSF error - IllegalStateException: PWC3999: Cannot

2020-02-26 13:50发布

I am new to JSF and I am building a application created with facelets.
Here is my template
master.xhtml

<body id="body">
 <form id="frmmaster">
        <div id="pg-nav-bg">
            <div class="wrapper clear">
                <div class="footer clear">
                    <div class="footerContent">Help Des</div>
                </div>
                <div id="pg-nav">
                    <ul class="nav sf-js-enabled">
                        <li class="page_item" id="liHomr">
                            <a href="#" title="Home">Home</a>
                        </li>
                        <li class="page_item" id="liFreeSearch">
                            <a href="#" title="Free Search" >Free Search</a>  
                        </li>
                        <li class="page_item" id="liMasterSearch">      
                            <a href="#"  title="Search">Search</a> 
                        </li>
                        <li class="page_item" id="liAdvanceSearch">
                            <a href="#" title="Advanced Search">Advanced Search</a> 
                        </li>
                        <li class="page_item" id="liCharts">
                            <a href="#" title="Charts" >Charts</a>  <!-- 
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="wrapper clear">
            <div class="Welcome ">
                <div class="welcome">
                     Welcome! &nbsp;<strong>Searcher</strong>
                    &nbsp; | &nbsp;
                    <strong >
                        [ <a title="Change Password" href="" style="color: #FFFFFF;">Change Password</a> ]
                        [ <h:commandLink id="lbSignOut" value="Sign Out" onclick="lbSignOut_Click"  styleClass="color: #FFFFFF;"></h:commandLink> ]
                    </strong>
                </div>
                <br class="clear" />
                <div>
                    <label id="lblMessage"></label>
                </div>
            </div>
        </div>
        <div class="wrapper big" >
            <div id="header" class="clear">
                <img src="../../images/logo.png" class="image" alt=""/>
                <div class="logo">
                    <div id="title" >M - 11</div>
                    <div id="version">V 0.8</div>
                    <div id="description">Helps in finding the right person</div>
                </div>
            </div>
            <!-- Here I included facelet -->
            <ui:include src="../SearchPages/MasterSearch.xhtml"/> 

        </div>
   </form>
</body>

</html>

Now the below file is my composition file that I included here in master.xhtml i-e

MasterSearch.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<div id="sidebar">
            <ul>
                <li class="widget">
                    <table>   
                    <h:form> 
                        <tr><td>Service Number</td>
                            <td>
                            <h:inputText id="txtMasterServiceNumber" styleClass="text-box" value="#{masterSearch.serviceNumber}" requiredMessage="enter value">
                            <f:validateRegex pattern="[A-Za-z0-9]{1,40}"/>
                            </h:inputText>
                            <h:message for="txtMasterServiceNumber" />

                            </td>
                        </tr>
                        <tr>
                            <td>Rank</td>
                            <td><h:selectOneMenu id="cboMasterRank" value="#{masterSearch.rank}" styleClass="select-field">
                                            <f:selectItem itemLabel="--Please Select--" itemValue="medium" />
                                            <f:selectItem itemLabel="A" itemValue="medium" />
                                            <f:selectItem itemLabel="B" itemValue="basic" />
                                            <f:selectItem itemLabel="C" itemValue="premium" />
                                            </h:selectOneMenu></td>
                        </tr>
                        <tr>
                            <td>Full Name</td>
                            <td>
                            <h:inputText id="txtMasterFullName" styleClass="text-box" value="#{masterSearch.fullName}" validatorMessage="Please enter a valid name.">
                            <f:validateRegex pattern="^([A-Za-z])*$" />
                            <h:message for="txtMasterFullName" styleClass="Error" /></h:inputText>
                            </td>
                        </tr>
                        <tr>
                            <td>CNIC Number</td>
                            <td><h:inputText id="txtMasterCNIC" value="#{masterSearch.CNICNumber}"  styleClass="text-box">
                            <f:validateRegex pattern="^([A-Za-z])*$" />
                            <h:message for="txtMasterFullName" styleClass="Error" />
                            </h:inputText>
                            </td>
                        </tr>
                        <tr>
                            <td>Phone No.</td>
                            <td><h:inputText id="txtMasterPhoneNo" value="#{masterSearch.phoneNumber}"  styleClass="text-box"></h:inputText>
                            </td>
                        </tr>
                        <tr>
                            <td>Email Address</td>
                            <td><h:inputText id="txtMasterEmail" value="#{masterSearch.emailAddress}"  styleClass="text-box"></h:inputText>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2" align="right">
                            <h:commandButton id="btnMasterSearch"  type="submit" value="Search" styleClass="submitButton"  action="#{masterSearch.findPerson}"></h:commandButton>
                            </td>
                        </tr>
                        </h:form>
                   </table>
                </li>
            </ul>
                <img src="../../images/loader.gif" alt="" style="vertical-align:middle;margin:2px;"/>Loading...
        </div>

    </div>
    <script src="../../Scripts/searchexternal.js" type="text/javascript"></script>

MasterSearch.java Which is a bean

public class MasterSearch {

    long serviceNumber;
    String rank;
    String fullName;
    String CNICNumber;
    String phoneNumber;
    String emailAddress;

    public long getServiceNumber() {
        return serviceNumber;
    }
    public void setServiceNumber(long serviceNumber) {
        this.serviceNumber = serviceNumber;
    }
    public String getRank() {
        return rank;
    }
    public void setRank(String rank) {
        this.rank = rank;
    }
    public String getFullName() {
        return fullName;
    }
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    public String getCNICNumber() {
        return CNICNumber;
    }
    public void setCNICNumber(String cNICNumber) {
        CNICNumber = cNICNumber;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public String getEmailAddress() {
        return emailAddress;
    }
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String findPerson(){

        return "personfound";

    }

}

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"

    version="2.0">

    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>

<managed-bean>
    <managed-bean-name>masterSearch</managed-bean-name>
        <managed-bean-class>
            MasterSearch
        </managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
    <managed-bean-name>masterResult</managed-bean-name>
        <managed-bean-class>
            com.delta.resultbeans.MasterSearchResult
        </managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<navigation-rule>
    <description>Navigation from the MasterSearch</description>
    <from-view-id>/WebPages/SearchPages/MasterSearch.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>personfound</from-outcome>
        <to-view-id>/WebPages/SearchPages/found.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-outcome>personnotfound</from-outcome>
        <to-view-id>/notfound.jsp</to-view-id>
    </navigation-case>
</navigation-rule>
</faces-config>

Now When I run this code (actually masterp.xhtml) it shows perfect output and loads accuratley but it throws an exception in console i-e

SEVERE: Error Rendering View[/WebPages/MasterPage/master.xhtml]
java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2885)
    at org.apache.catalina.connector.Request.getSession(Request.java:2582)
    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:419)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

INFO: Exception when handling error trying to reset the response.
java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2885)
    at org.apache.catalina.connector.Request.getSession(Request.java:2582)
    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:419)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2885)
    at org.apache.catalina.connector.Request.getSession(Request.java:2582)
    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:419)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

And I think due to this it is not submitting action of my button

<h:commandButton id="btnMasterSearch" type="submit" value="Search" styleClass="submitButton" action="#{masterSearch.findPerson}"> present in MasterSearch.xhtml.I found on SO that people had that problem but I am unable to find and understand a good solution.
please help thanks

1条回答
干净又极端
2楼-- · 2020-02-26 14:04

This is a known bug in Mojarra. It has been reported as issues 2215 and 2277 and has been fixed since Mojarra 2.1.8. So if you upgrade to at least 2.1.8, then this problem should disappear.

The cause of this problem can be explained as follows: Mojarra is trying to postpone the session creation as much as possible. The session should only be created at the moment when it is really needed. Among others, when the JSF view state of the <h:form> needs to be written, which normally happens during rendering of the closing </h:form>. However, on relatively large pages wherein the closing </h:form> appears after 2KB of written HTML, then it's too late to create the session. To create a session the server namely needs to set a cookie. However, to set a cookie, the response should not have been committed (the cookie namely needs to go in the response header). A server is by default configured to flush the response on every 2KB of written HTML.

As said, this issue is been fixed since Mojarra 2.1.8. You can download the latest version here.

查看更多
登录 后发表回答