2012-07-24 46 views
0

使用this解决方法我尝试登出。使用servlet在Java EE应用程序中登录用户

servlet代码:

@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()); 

    } 
} 

查看代码:

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

错误:

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

我不认为servle t以“/ logout”url模式接受请求。我做了什么不正确?

回答

1

在JSF中,action不是下一个要调用的servlet的URL。而不是它,它通过faces-config或直接从后台bean定义导航规则。

该消息告诉您,您的应用没有与您的.xhtml页面的动作logout匹配。

,你有一个ManagedBeanBackingBean与方法logout()和返回的URL“再见”地址我会做类似

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

注意:如果您想从servlet执行操作,您应该使用常规html标记(<a>,<button>而不是JSF组件)来链接到它。

+0

在所有页面中使用具有注销方法的单个支持bean是否公平?我可能有很多页面,并且每个页面调用相同的后备Bean方法 – kinkajou 2012-07-24 06:03:21

+0

是的。在任何页面中,您都可以使用多个支持bean;无论是数据还是行动。相反,您可以使用任何页面上的任何bean。 – SJuan76 2012-07-24 06:05:25

+0

+1 helpful :)你可以用标签和servlet提供简单的例子吗? – kinkajou 2012-07-24 06:10:44

相关问题