Jak przekierować do strony logowania, gdy sesja wygasa?

Mam trzy moduły internetowe JSF 2.0 i muszę przejść do strony logowania, gdy sesja wygasa.

Próbowałem go użyćHttpSessionListener, dzwonisessionDestroyed() metoda zdarzenia, ale nie mogę tam przesłać / przekierować żądania. Myślę, że to dlatego, że nie maHttpServletRequest iHttpServletResponse przedmioty.

Wypróbowałem to także za pomocą aPhaseListener, ale powoduje błąd „zbyt wiele przekierowań” w przeglądarce internetowej.

public class SessionListener implements PhaseListener {

    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }

    public void beforePhase(PhaseEvent event) {
        if (!FacesContext.getCurrentInstance().isPostback()) {
            try {
                System.out.println("Session Destroyed");
                FacesContext.getCurrentInstance().getExternalContext().redirect("login.jsf");
            }
            catch (Exception e) {
                System.out.println("error" + e);
            }
        }
    }

    public void afterPhase(PhaseEvent event)  {
        try {
            System.out.println("Session Created");
        }
        catch (Exception e) {
            System.out.println("error" + e);
        }
    }

}

Dlaczego te próby nie działają i jak najlepiej je rozwiązać?

questionAnswers(2)

yourAnswerToTheQuestion