-1

I have the following JSF backing bean method which should perform a redirect depending on some condition.

public void navigationStatus() throws IOException {
    log(LogMessages.getLogString(user, currentDatasource, nomeClasse, "navigationStatus", "navigationStatus"), null);
    logger.debug("INSIDE  navigationStatus ");
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

    if (!isUserEnabled()) {
        log(LogMessages.getLogString(user, currentDatasource, nomeClasse, "navigationStatus", "Sessione utente SCADUTA"), null);
        logger.debug("Sessione utente SCADUTA");
        response.sendRedirect("error.jsp");         
    }
}

This does however not work on all pages. It works on some pages, but not on other pages. How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
FrankTan
  • 1,626
  • 6
  • 28
  • 63

1 Answers1

0

This will definitely fail under the following conditions:

  1. The incoming HTTP request is an ajax request.
  2. The HTTP response is already committed.

To solve/avoid them, you would need to:

  1. Use the standard JSF API method to send a redirect instead of hauling the raw Servlet API from under the JSF's hoods which in turn knows nothing about the JSF context it is running in.

    context.getExternalContext().redirect("error.jsp");
    

    Then the incoming HTTP request is an ajax request, then the above approach will transparently return the proper XML response as expected by JSF ajax engine.

    In the future, whenever you need to import something from javax.servlet.* package inside a JSF backing bean, then you should immediately stop coding and ask yourself if there is really not a standard JSF way for the job you had in mind.

  2. Perform the job before the render response phase. If you're doing this in e.g. the constructor or @PostConstruct of a request/view scoped backing bean which is referenced for the first time about "halfway" the page, then it may be too late to send a redirect, because the first part of the HTTP response is already been sent to the client side. This is a point of no return and you would only get IllegalStateException all over place in the server logs.

    If this is indeed the case, then you need to invoke the job by a <f:event> listening on preRenderView.

    <f:event type="preRenderView" listener="#{bean.navigationStatus}" />
    
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thank you very much for the explination you are perfectly right beccause i was doing it in the constructor ! but why -1 ? to my question? – FrankTan Mar 26 '13 at 13:48
  • You're welcome. The -1 is most likely due to your carelessness (typos, etc) during formulating of the question and/or not really paying attention and love to the English language. It look too much like as if you couldn't be bothered to overcome like a professional. I have in any way improved/fixed the question for you :) – BalusC Mar 26 '13 at 13:50
  • f:event is available in jsf 1.2 ? if it is not , what can i use? – FrankTan Jul 08 '13 at 10:50
  • Use ``. See also http://stackoverflow.com/a/17514394/157882 In future questions, always explicitly mention JSF version. JSF 2.0 is out for more than 3 years already, so every question where version is unmentioned will just be assumed JSF 2.x. – BalusC Jul 08 '13 at 11:33
  • Stacktrace would be helpful to understand the cause. – BalusC Jul 08 '13 at 13:29