0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Is it fair to use single backing bean with logout method in all pages. I may have lot of pages and each page calling to same backing beans method
Yes. In any page you can use multiple backing beans; either for data or actions. And reciprocally, you can use any bean from any page.
+1 helpful :) Can you provide simple example with <a> tag and servlet?
Use the <a> tag as if it was plain HTML. In fact it is just the HTML tag.
Just found about h:outputLink, to generate the link directly from JSF. Needed if you need to pass a parameter value through EL. marceloagustini.wordpress.com/2012/04/24/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.