kinkajou
kinkajou

Reputation: 3728

Using servlet to log user out in Java EE application

Using this answer I tried to logout.

Servlet Code:

@WebServlet(name = "LogoutServlet", urlPatterns = {"/logout"})
public class LogoutServlet extends HttpServlet {
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(user.class);

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Destroys the session for this user.
        if (request.getSession(false) != null) {
            request.getSession(false).invalidate();
        }

        // Redirects back to the initial page.
        logger.warn(request.getContextPath());
        response.sendRedirect(request.getContextPath());

    }
}

View Code:

<h:form>
      <h:commandButton value="Logout" action="/logout"/>
</h:form>

Error:

Unable to find matching navigation case with from-view-id '/Admin/appManager.xhtml' for action '/logout' with outcome '/logout'

I don't think servlet is taking the request with "/logout" url pattern. what have I done incorrect?

Upvotes: 0

Views: 490

Answers (1)

SJuan76
SJuan76

Reputation: 24885

In JSF, the action is not the URL of the next servlet to be called. Rather than that, it defines navigation rules either through faces-config or directly from the backing beans.

The message tells you that your app has no matching for an action logout from your .xhtml page.

I would do something like

<h:commandButton value="Logout" action="#{backingBean.logout()}"/> 

where you have a ManagedBean BackingBean with the method logout() and which returns the URL to the "goodbye" address.

Note: If you want to do the operation from a servlet, you should use regular html tags (<a>, <button> instead of JSF components) to link to it.

Upvotes: 1

Related Questions