jsfbootsfaces

ServletException while trying to login again after logout in JSF


I'm struggling with a problem I have trying to perform a logout from a navbar. Everytime I logout from a Session and try to login again, after performing the login procedure the system just crashes with a ServletException error, with very little feedback.

When you first logout it performs smoothly, it just redirects you to the index and that's it. After you login again and try to perform the logout again, a pop-up window just appears, the one you get when trying to refresh a form filled with data for example.

The following code is in a .xhtml snippet, and it's the navCommandLink which is in charge of performing the logout.

                <b:navCommandLink iconAwesome="power-off"
                    action="#{navbar.logout()}">
                    <span style="margin-left: 10px;">Logout</span>
                </b:navCommandLink>

And the code of the action is the following one.

public String logout() {
    Map<String, Object> session = FacesContext.getCurrentInstance()
            .getExternalContext().getSessionMap();
    session.remove("LOGGEDIN_USER");

    FacesContext.getCurrentInstance().getExternalContext()
            .invalidateSession();
    return "index";
}

It should just return to the index.xhtml page and that's it.


Solution

  • Every question should have a proper answer, so let me repeat the answer given in the comments. The problem is that invalidating the session prevents you from triggering a JSF navigation. So you have to find an alternative, such as navigating via JavaScript or using externalContext.redirect(externalContext.getContextName() + "/index.xhtml");. Also read the comments for additional information.