首页 > 解决方案 > 如何处理登录表单中的 Servlet?

问题描述

我正在构建一个仅使用 JSP 页面和 Servlet 的 Web 应用程序。我正在尝试制作登录表单,但无法访问主页。有谁知道如何处理这个?

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

try {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    Iterator it = list_etudiant().iterator();
    System.out.println("username" + username);
    while (it.hasNext()) {
        Etudiant et = (Etudiant) it.next();
        String user_name = et.getEmailEtudiant();
        String pass_word = et.getPasswordEtudiant();
        if ((user_name == null ? username == null : user_name.equals(username)) && (pass_word == null ? password == null : pass_word.equals(password))) {

            response.sendRedirect("View/Home.jsp");
        }

    }

} catch (Exception ex) {
    Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
    response.sendRedirect("index.html");
}

} 

还有一个问题,为什么我在 Netbeans 控制台中看不到输出?例如,当我想打印一些东西时:

System.out.println("username is : "+username);

我找不到输出,我只是得到这个:

BUILD SUCCESSFUL (total time: 1 second)

标签: formsjspservletsloginconsole

解决方案


if ((user_name == null ? username == null : user_name.equals(username)) && 
            (pass_word == null ? password == null : pass_word.equals(password))) {

        response.sendRedirect("View/Home.jsp");
    }

我希望你的其余代码工作正常。参考上面的代码片段。我猜你的观点是用户名和密码不应该为空并且需要相等。

        if (((user_name != null) && (user_name.equals(username)))
            && ((pass_word != null) && (pass_word.equals(password)))) {
        System.out.println("username is : " + username);  // If you need to print it on console
        response.sendRedirect("View/Home.jsp");
    }

您的条件检查不正确。请参考?:运算符


推荐阅读