在提交响应后无法创建会话



打开JSF页面会出现以下异常:

由于:java.lang.IllegalStateException: Cannot create a session after response has committed

以下是JSF代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.prime.com.tr/ui">
    <h:head>
        <h:outputStylesheet name="master_c.css" library="css" />
        <title>Quotes</title>
    </h:head>
    <h:body styleClass="venue product">
        <h:form>
            <div id="wrapper">
                <div id="content">
                    <h2><a href="#"><img src="/Webify/static/image/venue-large.png" /></a></h2>
                    <ul id="calltoaction">
                        <li id="cta-map"><a href="#">MAP</a></li>
                        <li id="cta-download" class="addthis_toolbox addthis_share_btn"><a href="#">qmobile</a></li>
                    </ul>
                    <!-- calltoaction -->
                    <div class="sec-content">
                        <ul id="techspecs" class="mobile">
                            <li></li>
                            <li><img height="50" width="79" alt="QMobile Kâşif" src="/Webify/static/image/mac-universal.png" /><b>QMobile</b><br />Bahara<br />Pahli baar</li>
                            <li>Lorem Ispum Lorem Ispum Lorem Ispum Lorem Ispum Lorem Ispum Lorem Ispum Lorem Ispum Lorem Ispum Lorem Ispum Lorem Ispum Lorem Ispum Lorem Ispum</li>
                        </ul>
                        <!-- techspecs -->
                        <ul id="sc-reviews" class="mobile">
                            <li><a href="#"><img height="35" width="200" alt="Android" src="/Webify/static/image/text_android.png" /></a></li>
                            <li><a href="#"><img height="35" width="200" alt="IPhone" src="/Webify/static/image/text_iphone.png" /></a></li>
                            <li><a href="#"><img height="35" width="200" alt="BlackBerry" src="/Webify/static/image/text_blackberry.png" /></a></li>
                            <li><a href="#">Find More...</a></li>
                        </ul>
                        <!-- sc-reviews -->
                    </div>
                    <div class="prim-content">
                        <!-- Page Heading -->
                        <div id="sec-navigation">
                            <h3 style="margin-left: 10px;"></h3>
                        </div>
                        <div id="wall">
                            <!-- bricks end -->
                        </div>
                        <!-- wall end -->
                    </div>
                </div>
            </div>
        </h:form>
    </h:body>
</html>

下面是堆栈跟踪:

Sep 16, 2011 1:45:40 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
SEVERE: Error Rendering View[/streamfaces.xhtml]
java.lang.IllegalStateException: Cannot create a session after the response has been committed
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2734)
    at org.apache.catalina.connector.Request.getSession(Request.java:2244)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:899)
    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 com.ocpsoft.pretty.faces.application.PrettyViewHandler.renderView(PrettyViewHandler.java:163)
    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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
Sep 16, 2011 1:45:40 PM com.sun.faces.context.ExceptionHandlerImpl throwIt
INFO: 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:2734)
    at org.apache.catalina.connector.Request.getSession(Request.java:2244)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:899)
    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 com.ocpsoft.pretty.faces.application.PrettyViewHandler.renderView(PrettyViewHandler.java:163)
    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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
Sep 16, 2011 1:45:40 PM com.sun.faces.renderkit.RenderKitUtils renderHtmlErrorPage
WARNING: JSF1087: Unable to generate Facelets error page as the response has already been committed.
Sep 16, 2011 1:45:40 PM com.sun.faces.renderkit.RenderKitUtils renderHtmlErrorPage
SEVERE: javax.faces.FacesException: Cannot create a session after the response has been committed
javax.faces.FacesException: Cannot create a session after the response has been committed
    at com.sun.faces.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:141)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.IllegalStateException: Cannot create a session after the response has been committed
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2734)
    at org.apache.catalina.connector.Request.getSession(Request.java:2244)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:899)
    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 com.ocpsoft.pretty.faces.application.PrettyViewHandler.renderView(PrettyViewHandler.java:163)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    ... 19 more

这个问题是如何产生的,我该如何解决它?我使用Mojarra 2.1.2。

原因是:

    JSF将页面组件结构存储在HttpSession 中。
  1. 如果页面内容非常大,并且之前没有创建会话,则可能已经编写了一些响应并发生上述错误
因此,解决方案是在打印Facelet (/JSP)页面之前创建会话。一个简单的例子可以是:
@PostConstruct
void initialiseSession() {
    FacesContext.getCurrentInstance().getExternalContext().getSession(true);
}

/矮麝香鹿

[SOLVED]

诊断这个问题花了我几天的时间,但最终我能够解决这个问题。

它实际上是@ViewScoped,它有一个未序列化的托管属性DAO对象。所以在视图被渲染之后,facelets也会产生这个异常,但是JSF视图已经渲染了,这就是为什么会显示这个异常。

解决方案:使所有对象在@ViewScoped bean中序列化,并标记未序列化的"transient",就像在我的情况下,它是JSF @ViewScoped中的DAO对象。

我把它放在我的web.xml中,以解决我的一个应用程序的问题。

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

奇怪的是,我在同一个glass fish实例上托管了几个其他应用程序,这些应用程序不需要在web.xml中执行此操作。

我强烈希望相信这不是解决方案,因为它没有任何意义对我来说,为什么这个特定的应用程序需要它,而其他不需要。但是现在它还能用

我也是这样。

我曾经有Mojarra 2.1.13和更新到2.1.29后,问题自行解决,没有任何更改我的代码

这个错误也发生在我身上,但与创建自定义错误页面有关。我已经生成了一个简单的错误页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:jsf="http://xmlns.jcp.org/jsf"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
    <f:view contentType="text/html;charset=UTF-8" encoding="UTF-8">
        <h:head>
        </h:head>
        <h:body>
            <!-- start writing here your components -->
        </h:body>
    </f:view>
</html>

我已经配置了web.xml来发送404错误到这个页面:

<error-page>
  <error-code>404</error-code>
  <location>/error_404.xhtml</location>
</error-page>

当我试图激发错误时,我碰到了相同的错误

相关内容

  • 没有找到相关文章