java.lang.IllegalStateException en com.sun.faces.context.FacesContextImpl.assertNotReleased

Si presiono el botón Enviar, los mensajes de error se lanzan al registro del servidor

login.xhtml

  

Login.java

 @ManagedBean @ViewScoped public class Login { private FacesContext fCtx; private String username; private String password; public Login() { fCtx = FacesContext.getCurrentInstance(); } public String login(){ // HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(true); // String sessionId = session.getId(); fCtx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Info: ", getUsername()+", "+getPassword()+", ")); return "start.xhtml"; } public void setUsername(String username) { this.username = username; } public String getUsername() { return username; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } } 

Error Stack Trace

 05.01.2011 15:24:00 com.sun.faces.application.ActionListenerImpl processAction SCHWERWIEGEND: java.lang.IllegalStateException javax.faces.el.EvaluationException: java.lang.IllegalStateException at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98) at javax.faces.component.UICommand.broadcast(UICommand.java:311) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306) 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:161) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281) 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 at com.sun.faces.context.FacesContextImpl.assertNotReleased(FacesContextImpl.java:635) at com.sun.faces.context.FacesContextImpl.addMessage(FacesContextImpl.java:471) at org.dhbw.stg.wwi2008c.mopro.ui.managedBeans.Login.login(Login.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.el.parser.AstValue.invoke(AstValue.java:262) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84) ... 23 more 05.01.2011 15:24:00 com.sun.faces.lifecycle.InvokeApplicationPhase execute WARNUNG: #{login.login}: java.lang.IllegalStateException javax.faces.FacesException: #{login.login}: java.lang.IllegalStateException at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:114) at javax.faces.component.UICommand.broadcast(UICommand.java:311) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306) 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:161) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281) 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: javax.faces.el.EvaluationException: java.lang.IllegalStateException at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98) ... 22 more Caused by: java.lang.IllegalStateException at com.sun.faces.context.FacesContextImpl.assertNotReleased(FacesContextImpl.java:635) at com.sun.faces.context.FacesContextImpl.addMessage(FacesContextImpl.java:471) at org.dhbw.stg.wwi2008c.mopro.ui.managedBeans.Login.login(Login.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.el.parser.AstValue.invoke(AstValue.java:262) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84) ... 23 more javax.faces.FacesException: #{login.login}: java.lang.IllegalStateException at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:85) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306) 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:161) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281) 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: javax.faces.FacesException: #{login.login}: java.lang.IllegalStateException at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:114) at javax.faces.component.UICommand.broadcast(UICommand.java:311) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77) ... 18 more Caused by: javax.faces.el.EvaluationException: java.lang.IllegalStateException at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98) ... 22 more Caused by: java.lang.IllegalStateException at com.sun.faces.context.FacesContextImpl.assertNotReleased(FacesContextImpl.java:635) at com.sun.faces.context.FacesContextImpl.addMessage(FacesContextImpl.java:471) at org.dhbw.stg.wwi2008c.mopro.ui.managedBeans.Login.login(Login.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.el.parser.AstValue.invoke(AstValue.java:262) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84) ... 23 more 

¿Cuál es el problema de cómo puedo solucionarlo?

Tu error está aquí:

 @ManagedBean @ViewScoped public class Login { private FacesContext fCtx; public Login() { fCtx = FacesContext.getCurrentInstance(); } } 

Nunca debe asignar FacesContext como variable de instancia de un bean administrado de vista / sesión / aplicación con ámbito y reutilizarlo en diferentes solicitudes, porque la instancia actual de FacesContext está vinculada a la solicitud actual, lo que hace que inherentemente solicite un ámbito (pero preferiblemente también don ‘ t así que en frijol con scope solicitado debido a un estilo pobre que puede ser confuso para los principiantes).

La instancia de FacesContext se libera al final de la solicitud. En solicitudes posteriores, la instancia obtenida en una solicitud anterior ya no es válida. Si hubiera funcionado, todos los métodos getter en ExternalContext le habrían devuelto las propiedades (parámetros, cookies, encabezados, etc.) de una solicitud HTTP anterior y todos los métodos setter configurados en una respuesta HTTP anterior que ya se había comprometido por mucho tiempo. Esto simplemente no está bien.

Siempre debe obtenerlo por subprocesos locales (es decir, directamente dentro del bloque de métodos). En su caso específico, simplemente obténgalo directamente dentro del método de login() :

 public String login() { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info: ", getUsername() + ", " + getPassword() + ", ")); return "start.xhtml"; }