title: JSF sendRedirect() java.lang.IllegalStateException: Committed
body: <p>I am creating a web page that get an <strong>id</strong> from a previous page using getSessionMap() in FacesUtils. If the page did not get the <strong>id</strong>, it should be redirected to other page, in this case <strong>draft.jsf</strong>. Here is the code:</p>
  
  <pre><code>if ((FacesUtils.getSessionMap().get("id") == null)) {
      try {
          FacesUtils.getResponse().sendRedirect("draft.jsf");
          return;         
      } catch (Exception e) {
          e.printStackTrace();
  }
  } else {
      id = Long.valueOf(String.valueOf(FacesUtils.getSessionMap().get("id")));
  }
  </code></pre>
  
  <p>But I always get <strong>java.lang.IllegalStateException: Committed</strong> when the id was <strong>null</strong>, and the page was not redirected to <strong>draft.jsf</strong>. What is wrong with my code? Any help would be really appreciated.</p>
  
  <p>Here is the stacktrace:</p>
  
  <pre><code>java.lang.IllegalStateException: Committed
  at org.mortbay.jetty.Response.resetBuffer(Response.java:1023)
  at org.mortbay.jetty.Response.sendRedirect(Response.java:428)
  at org.jleaf.erp.inv.controller.EditAssemblyController.init(EditAssemblyController.java:58)
  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.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:340)
  at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:293)
  at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:130)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:393)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1415)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:518)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
  at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:331)
  at org.jlego.web.jsf.ViewScope.get(ViewScope.java:20)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:327)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
  at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
  at org.jlego.baseweb.PieceletELResolver.getValue(PieceletELResolver.java:65)
  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:99)
  at com.sun.el.parser.AstValue.getValue(AstValue.java:158)
  at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
  at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
  at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
  at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
  at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
  at org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:69)
  at org.primefaces.component.inputtext.InputTextRenderer.encodeMarkup(InputTextRenderer.java:79)
  at org.primefaces.component.inputtext.InputTextRenderer.encodeEnd(InputTextRenderer.java:50)
  at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
  at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
  at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
  at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
  at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
  at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:55)
  at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:43)
  at org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:191)
  at org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:114)
  at org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:55)
  at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
  at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
  at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
  at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
  at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
  at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
  at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
  at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
  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.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1221)
  at org.jlego.web.WebLoginSessionFilter.doFilter(WebLoginSessionFilter.java:95)
  at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
  at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
  at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
  at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
  at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
  at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
  at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
  at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
  at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
  at org.mortbay.jetty.Server.handle(Server.java:326)
  at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
  at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
  at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
  at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
  at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
  at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
  at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
  </code></pre>