title: java.lang.IllegalStateException when trying to navigate between two Java EE applications
body: <p>Basically i have a enterprise jsf application which shows some services to the user.</p>
  
  <p>Different departments of the enterprise host different applications on single webserver and links are provided in our application to access them.</p>
  
  <p>I have a <code>Servlet</code> named RedirectServlet in my application through which i am redirecting all the requests to different applications.The following is my redirectservlet</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          LOGGER.debug("In doGet()");
          try{
              if(request.getParameter("URL") != null){
                  LOGGER.debug("URL Found redirecting forward");
                  String redirectUrl = request.getParameter("URL"); 
                  response.sendRedirect(redirectUrl);
              }
          }catch(NullPointerException e){
              LOGGER.debug("URL not found setting new session parameters &amp; redirecting forward");
              HttpSession session = request.getSession(true);
              try {
                  response.sendRedirect(properties.getProperty(REDIRECTURL, true));
              } catch (Exception e1) {
                  LOGGER.debug("Expcetion with propertiesfile: ",e1);
              }
          }
      }
  </code></pre>
  
  <p>What i do above is if a link is clicked(URL parameter is present) i redirect to specific application, otherwise i just create a new session of my application and redirect to my application homepage.</p>
  
  <p>when the user logsout from any other application, i am redirecting back to my application
  This is how i am redirecting back to my application</p>
  
  <pre><code>public void Logout(){
          LOGGER.debug("In LogOut");
          try
          {
              HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
              HttpServletResponse res = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
              HttpSession session = req.getSession(false);
              LOGGER.debug("report session id:" + session.getId());
              res.sendRedirect(REDIRECTURL);
              session.invalidate();
          }catch(IllegalStateException e){
              LOGGER.debug("Exception in LogOut", e);
          }
          catch (IOException e){
              LOGGER.debug("Exception in LogOut", e);
          }
      }
  </code></pre>
  
  <p>i am getting the following server error message at <code>res.sendRedirect</code> line in the above code</p>
  
  <pre><code>HTTP Status 500 -
  
  type Exception report
  
  message
  
  description The server encountered an internal error () that prevented it from fulfilling this request.
  
  exception
  
  javax.servlet.ServletException: Cannot forward after response has been committed
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:221)
  root cause
  
  java.lang.IllegalStateException: Cannot forward after response has been committed
      org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(ServletExternalContextImpl.java:368)
      org.apache.myfaces.trinidad.context.ExternalContextDecorator.dispatch(ExternalContextDecorator.java:44)
      org.apache.myfaces.trinidad.context.ExternalContextDecorator.dispatch(ExternalContextDecorator.java:44)
      org.apache.myfaces.trinidadinternal.context.FacesContextFactoryImpl$OverrideDispatch.dispatch(FacesContextFactoryImpl.java:267)
      org.apache.myfaces.view.jsp.JspViewDeclarationLanguage.buildView(JspViewDeclarationLanguage.java:94)
      org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:66)
      org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
  </code></pre>
  
  <p>This is how we are managing session between applications</p>
  
  <p>When the user first login to our application, we store in the some details in DB and after that every applications checks in the DB for that details.</p>
  
  <p>Please somebody help how to solve this.This is time critical</p>
  
  <p>Both the applications are using <code>JSF 1.2</code> and <strong>Apache Trinidad</strong>.</p>