2

I developed a Java Web Application with JSF 2.2.12, Prime Faces 6.0, Omnifaces 2.4 and Glassfish 4.1.1.

If the user doesn't have a session and try to access to the website through a page different than the home page this error is shown

Unable to set property SessionScopedBean for managed bean ContactController ... ... Caused by: java.lang.IllegalStateException: Cannot create a session after the response has been committed

I've saw some other questions regarding "Cannot create a session after the response has been committed" and I've saw some "solutions".

At the moment the most "effective" is this answer https://stackoverflow.com/a/9687525/3564882. Yes, the error disappears, but the drawback is that the user is redirected to the home page. For me is not clear what's happening here.

Here some pieces of code but all the beans doesn't do anything of special

ContactController

@ManagedBean
@ViewScoped
public class ContactController implements Serializable {

@ManagedProperty(value = "#{sessionScopedBean}")
private SessionScopedBean sessionScopedBean;

public void loadThePage() {
    UserInSessionBean userInSessionBean = sessionScopedBean.getUserInSessionBean();
    if (userInSessionBean != null) {
        ...
    }
}

SessionScopedBean

@ManagedBean(name = "sessionScopedBean")
@SessionScoped
public class SessionScopedBean implements Serializable{
   private UserInSessionBean userInSessionBean;
   ...
   ...
   public UserInSessionBean getUserInSessionBean() {
        return userInSessionBean;
   }

   public void setUserInSessionBean(UserInSessionBean userInSessionBean) {
        this.userInSessionBean = userInSessionBean;
   }
}

So basically the problem happens when my user doens't have the session and try to access to a page different than the home page. In all the other cases I don't have any problem.

Here the complete stacktrace

Warning:   Unable to set property sessionScopedBean for managed bean contactController
com.sun.faces.mgbean.ManagedBeanCreationException: Unable to set property sessionScopedBean for managed bean contactController
    at com.sun.faces.mgbean.ManagedBeanBuilder$BakedBeanProperty.set(ManagedBeanBuilder.java:615)
    at com.sun.faces.mgbean.ManagedBeanBuilder.buildBean(ManagedBeanBuilder.java:133)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:257)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:117)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:116)
    at com.sun.el.parser.AstValue.getBase(AstValue.java:151)
    at com.sun.el.parser.AstValue.getTarget(AstValue.java:170)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:272)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UIViewAction.broadcast(UIViewAction.java:562)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.glassfish.tyrus.servlet.TyrusServletFilter.doFilter(TyrusServletFilter.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at com.myApp.jsf.filter.SecurityWebFilter.doFilter(SecurityWebFilter.java:42)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at com.classicvirus.jsf.filter.LoggedUserWebFilter.doFilter(LoggedUserWebFilter.java:55)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Cannot create a session after the response has been committed
    at org.apache.catalina.connector.Request.doGetSession(Request.java:3241)
    at org.apache.catalina.connector.Request.getSession(Request.java:2884)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:908)
    at com.sun.faces.context.ExternalContextImpl.getSession(ExternalContextImpl.java:167)
    at javax.faces.context.ExternalContextWrapper.getSession(ExternalContextWrapper.java:438)
    at com.sun.faces.mgbean.BeanManager$ScopeManager$SessionScopeHandler.handle(BeanManager.java:578)
    at com.sun.faces.mgbean.BeanManager$ScopeManager.pushToScope(BeanManager.java:458)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:410)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:257)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:117)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:116)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.mgbean.BeanBuilder$Expression.evaluate(BeanBuilder.java:561)
    at com.sun.faces.mgbean.ManagedBeanBuilder$BakedBeanProperty.set(ManagedBeanBuilder.java:606)
    ... 67 more

Can you try to help me?

Community
  • 1
  • 1
Gavi
  • 1,300
  • 1
  • 19
  • 39

1 Answers1

0

After some investigation I've realized that was a mix between my mistake and (maybe) a JSF bug.

Regarding the following

At the moment the most "effective" is this answer http://stackoverflow.com/a/9687525/3564882. Yes, the error disappears, but the drawback is that the user is redirected to the home page. For me is not clear what's happening here.

was my fault because in the @PostConstruct method in the past I was redirecting the user to the home page. In my ideal scenario, the new code creates a bug.

But for me is not clear why we need to create the session if needed. Sometimes JSF need it. Sometimes not. For this reason I think this is a little bug inside JSF.

Just to recap. The solution is to place this line

FacesContext.getCurrentInstance().getExternalContext().getSession(true);

in all the controllers(I've a superclass that helped me).

After this, remember to create the "sessionScopedBean" thinking that the case explained in the question exists. In this way you will avoid to waste some hours.

Gavi
  • 1,300
  • 1
  • 19
  • 39