web-dev-qa-db-fra.com

Impossible de créer une session après la validation de la réponse

L'ouverture de la page JSF me donne l'exception suivante:

Causé par: Java.lang.IllegalStateException: impossible de créer une session après la validation de la réponse

Voici le code 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>

Voici le stacktrace:

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

Comment ce problème est-il provoqué et comment puis-je le résoudre? J'utilise Mojarra 2.1.2.

17
Faisal Basra

[RESOLU]

Il a fallu des jours pour diagnostiquer ce problème, mais finalement je suis en mesure de résoudre ce problème.

C'était en fait @ViewScoped et il possède un objet DAO de propriété gérée qui n'a pas été sérialisé. Donc, une fois la vue rendue, les facettes produisent également cette exception, mais la vue JSF est déjà rendue et c'est pourquoi cette exception est apparue.

Solution: Rendre tous les objets sérialisés en @ViewScoped bean, et marquez "transitoire" qui ne sont pas sérialisés comme dans mon cas c'était un objet DAO dans JSF @ViewScoped.

16
Faisal Basra

La raison en est que:

  1. JSF stocke la structure du composant de page dans HttpSession
  2. Si le contenu de la page est très volumineux et que la session n'a pas été créée précédemment, une réponse peut avoir été écrite et l'erreur ci-dessus se produit

La solution consiste donc à créer la session avant l'impression de la page Facelet (/ JSP). Un exemple grossier pourrait être comme:

@PostConstruct
void initialiseSession() {
    FacesContext.getCurrentInstance().getExternalContext().getSession(true);
}

/ napu

18
napu

Je l'ai mis dans mon web.xml pour résoudre le problème pour l'une de mes applications.

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

Ce qui est étrange cependant, c'est que j'ai plusieurs autres applications hébergées sur cette même instance de fish de verre qui ne nécessite pas ce vaudou dans le web.xml.

Je veux fermement croire que ce n'est pas la solution, car cela n'a aucun sens pour moi pourquoi cette application en particulier l'exige et les autres non. Mais pour l'instant ça marche.

Ça m'arrivait aussi.

J'ai utilisé Mojarra 2.1.13 et après l'avoir mis à jour vers 2.1.29, le problème s'est résolu sans aucune modification de mon code.

2
Adriano Castro