Servlet For Loop导致Http 500错误

时间:2015-05-11 10:31:22

标签: java html jsp servlets

我正在创建一个登录servlet。我不知道为什么,但我的for循环导致它显示500级错误。任何帮助将不胜感激。 创建的类只是一个带有getter和setter的基本类。

public Login() {
    super();

}

boolean isNameValid;
boolean isPassValid;

public void init(ServletConfig config) throws ServletException {
    super.init(config);
    ServletContext context = this.getServletContext();
    if (context.getAttribute("HomeworkUsers") == null) {
        ArrayList<CS320User> HomeworkUsers = new ArrayList<CS320User>();
        getServletContext().setAttribute("HomeworkUsers", HomeworkUsers);

        CS320User user1 = new CS320User("John", "Doe", "john@doe.com", "1!");
        CS320User user2 = new CS320User("Joe", "Boxer", "joe@boxer.com",
                "2@");

        HomeworkUsers.add(user1);
        HomeworkUsers.add(user2);

        context.setAttribute("HomeworkUsers", HomeworkUsers);
    }
}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    ServletContext context = this.getServletContext();

    ArrayList<CS320User> HomeworkUsers = (ArrayList<CS320User>) context
            .getAttribute("HomeworkUsers");

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<!doctype html>");
    out.println("<html>");
    out.println("<head>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h3>Login Servlet</h3>");

    out.println("<form action=\"Login\" method=\"post\">");

    out.println("Username: <input type='text' name='Username'/>");
    out.println("<br/>");
    out.println("Create Password: <input type='password' name= 'password' /> ");
    out.println("<br/>");
    out.println("<br/>");
    out.println("<label for =\"Remember Me\"> Remember Me");
    out.println("<input type=\"checkbox\" id= \"Remember Me\">");
    out.println("</label>");
    out.println("<br/>");

    out.println("<input type='submit' name='login' value='Login' /> ");

    out.println("<br>");

    for (CS320User users : HomeworkUsers) {
        if (users.getEmail().contains(request.getParameter("Username"))
                && users.getPassword().contains(
                        request.getParameter("password"))) {

            out.println(users.getEmail());
            out.println(users.getFirst());
            isNameValid = true;
            isPassValid = true;
        }
    }
    String Username = request.getParameter("Username");
    String password = request.getParameter("password");
    out.println(Username);
    out.println(password);
    out.println(isNameValid);
    out.println(isPassValid);
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");
}

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

    if (isNameValid == true && isPassValid == true) {
        response.sendRedirect("Welcome");
    }
    doGet(request, response);

}

1 个答案:

答案 0 :(得分:0)

如果您没有记录异常,我的意思是如果您不处理它们,您可以按预期在运行时获得500个错误。

要防止这种情况,只需使用try catch块并在catch块中处理异常。 (至少记录下来)

在您的代码中,只有一件事可能导致for循环行中出现异常,并且 HomeworkUsers 的可能性为空

ArrayList<CS320User> HomeworkUsers = (ArrayList<CS320User>) context
            .getAttribute("HomeworkUsers");

在我在顶部写的代码部分中,您将获得一个上下文属性,它可以为null。可能你的解决方案是检查它是否为空。