title: JSF navigation redirect to previous page
body: <p>Once the user successful login to the system, the system will redirect the user to the homepage. Now my problem is, if the user clicks on the view account page without login to the system the system will redirect user to the login page. if user login to the system now the system will redirect the user to the homepage, in this case any method can redirect user to the previous page that is view account page instead of homepage?</p>
  
  <p>i tried using session</p>
  
  <pre><code>String url = (String)session.getAttribute("url");
  if(url != null)
      response.sendRedirect(url);
  else
      response.sendRedirect("homepage.faces");
  </code></pre>
  
  <p>i put this code under public void doBtnAction(){} if the user login successful then redirect to the url. But i got this error </p>
  
  <pre><code>java.lang.IllegalStateException: Cannot forward after response has been committed
      com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
      com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:130)
      com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
      com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
      com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
  </code></pre>