无法显示多个错误消息.Java Servlet

sot*_*oto 1 java jsp servlets

我正在使用Java servlet和JSP创建一个登录页面.我只是发布了我的doPost方法和validateData方法.我担心的是我想在页面上输出两个以上的错误消息.errorMessageList是负责保存String消息的数组.最初,我试图通过在两个字段中输入数字来输入无效的名字和姓氏.相应的错误消息存储在数组中,但不会显示.仅显示第一个错误消息,即错误:无效的名字.不能为空/包含数字,但下一个错误错误:姓氏无效.即使errorMessageList已经存在,也不能显示空/包含数字.我也提供我的JSP.一些帮助将不胜感激!

     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    // We are creating all the parameters on the browsers and sending them to the server 
    String uName = req.getParameter("userName");
    String pWord = req.getParameter("password");
    String cPass = req.getParameter("confirmPassword");
    String fName = req.getParameter("firstName");
    String lName = req.getParameter("lastName");
    String gender = req.getParameter("gender");

    Client client = new Client();
    client.setClientName(uName);
    client.setPassword(pWord);
    client.setConfirmPassword(cPass);
    client.setFirstName(fName);
    client.setLastName(lName);
    client.setGender(gender);


    List<String> errorMessageList = validateServlet(client);
    boolean isEmpty = errorMessageList.isEmpty(); 
    System.out.println("the errorMessageList is empty:"+isEmpty);

    if(errorMessageList.size()==0){//Successful registration

        ClientDao clientDao = new ClientDaoImpl();
        clientDao.createClient(client);
        req.getRequestDispatcher("/WEB-INF/jsp/registrationSuccess.jsp").forward(req, resp);

    }

    // Error in the submission data. Therefore show registration page. 
    for(int i = 0; i < errorMessageList.size(); i++){
    req.setAttribute("errorMessages", errorMessageList.get(i));
    req.getRequestDispatcher("/WEB-INF/jsp/registration.jsp").forward(req, resp);
    }


    super.doPost(req, resp);
}

public List<String> validateServlet(Client client){

    List<String> errorMessages = new ArrayList<String>();


    if(client.getClientName() == null || client.getClientName().trim().length() == 0){// Null or Empty

        errorMessages.add("Error: Your forgot to type in User Name");

    }

    if(client.getFirstName() == null || client.getFirstName().trim().length() == 0 || client.getFirstName().matches(NUMBERS_PATTERN)){

        errorMessages.add("Error: Invalid First Name. Cannot be empty / contain numbers");

    }
    if(client.getLastName() == null || client.getLastName().trim().length() == 0 || client.getLastName().matches(NUMBERS_PATTERN)){

        errorMessages.add("Error: Invalid Last Name. Cannot be empty / contain numbers");

    }else if(!client.getClientName().matches(EMAIL_PATTERN)){

        errorMessages.add("Error: Invalid user. User Name should be user@domain.com");

    }else if(client.getPassword() == null || client.getPassword().trim().length() == 0){// Null or Empty

        errorMessages.add("Error: Your forgot to type in Password");

    }else if(client.getConfirmPassword() == null || client.getConfirmPassword().trim().length() == 0){// Null or Empty

        errorMessages.add("Error: Your forgot to confirm password");

    }else if(!client.getPassword().equals(client.getConfirmPassword())){

        errorMessages.add("Error: Password and confirm password do not match..");

    }       
    return errorMessages;
}


 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Client Registration Page</title>
</head>
<body style="background-color:#EEFFEE" align="center">
<%

   String eMessage = (String)request.getAttribute("errorMessages");
%>
<form action = "/iservlet-webapp/registration" method = "post">
     <%if(eMessage != null){ %>
        <div style = "color:#CC3300; align:center">
            <%=eMessage%>
        </div>  
     <%} %>


<font face="Arial" color="green">User Registration Page</font>
<hr>
<br>
<table align="center" border="1" bordercolor= "green" bgcolor="CCFF99">
    <tr>
        <td>User Name</td>
        <td><input type ="text" name ="userName" maxlength = "50"/></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><input type ="password" name ="password" maxlength = "15"/></td>
    </tr>
    <tr>
        <td>Confirm Password</td>
        <td><input type ="password" name ="confirmPassword" maxlength = "45"/></td>
    </tr>
    <tr>
        <td>First Name</td>
        <td><input type ="text" name ="firstName" maxlength = "45"/></td>
    </tr>
    <tr>
        <td>Last Name</td>
        <td><input type ="text" name ="lastName" maxlength = "45"/></td>
    </tr>
    <tr>
        <td>Gender</td>
        <td><input type ="text" name ="gender" maxlength = "1"/></td>
    </tr>
    <tr>
        <td colspan="2"><input type = "submit" name ="Login"/></td>
    </tr>
</table>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Nee*_*ain 5

 // Error in the submission data. Therefore show registration page. 
    for(int i = 0; i < errorMessageList.size(); i++){
    req.setAttribute("errorMessages", errorMessageList.get(i));
    req.getRequestDispatcher("/WEB-INF/jsp/registration.jsp").forward(req, resp);
    }
Run Code Online (Sandbox Code Playgroud)

通过上面的代码,你只设置1 error是在0th列表的索引,因为要调用forward()每个迭代法.

相反,您应该添加完整errorList的属性:

req.setAttribute("errorMessages", errorMessageList);
req.getRequestDispatcher("/WEB-INF/jsp/registration.jsp").forward(req, resp);
Run Code Online (Sandbox Code Playgroud)

现在只需在JSP中迭代列表.


更新

如何在JSP中迭代列表?

<c:forEach items="${errorMessages}" var="error">
    <h2>${error}</h2>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)

要使用,<c:forEach>您需要jstl.jar在类路径中添加并在jsp中添加以下行.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Run Code Online (Sandbox Code Playgroud)