使用登录信息从Servlet打开JSF页面

时间:2013-05-27 10:17:01

标签: jsf servlets

遵循BalusC指南我尝试创建一个Login系统,以便通过Servlet访问我的JSF页面,但它不起作用:(

我的登录Servlet是:

public class DoLogin  extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    creaLogin(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    creaLogin(request, response);
}

//This method call the FacesContext and pass login nformation
private void creaLogin(HttpServletRequest request, HttpServletResponse response){
    FacesContext facesContext = FaceUtil.getFacesContext(request, response);

    //Retrieve login information from get/post callback
    String userid=request.getParameter("userid");
    String password=request.getParameter("password");

    //I create an istance of LoginBean backingbean that manage login information for my JSF page
    LoginBean mioBean = (LoginBean) facesContext.getApplication().evaluateExpressionGet(facesContext, 
            "#{loginBean}", LoginBean.class);
    mioBean.setUsername(userid);
    mioBean.setPassword(password);
    mioBean.externalLogin();   //this method perform all needed operation and intialize all backing beans of my app

    //Call ma xhtml page 
    ConfigurableNavigationHandler nav 
       = (ConfigurableNavigationHandler) 
               facesContext.getApplication().getNavigationHandler();

    nav.performNavigation("pannello");

}

}

FaceUtil与BalusC在他的例子中所表现的相同。

提前谢谢

问候

1 个答案:

答案 0 :(得分:0)

您不应将servlet视为JSF辅助bean。 JSF是一个基于Servlet API构建的MVC框架。如果由于某些原因而无法使用完整的JSF支持bean替换servlet,那么您应该使用“普通”Servlet API完全相同,因为JSF正在FacesContext的封面下进行。

例如,

LoginBean loginBean = new LoginBean();
request.getSession().setAttribute("loginBean", loginBean); // Puts bean in session scope.
loginBean.setUsername(userid);
loginBean.setPassword(password);
loginBean.externalLogin(); // Note: if this has CDI/EJB dependencies, just refactor and inject them in servlet instead.

response.sendRedirect(request.getContextPath() + "/pannello.xhtml"); // Assuming FacesServlet is mapped on *.xhtml.