当服务器端验证消息显示在jsp中的相应字段时,如何在输入字段中保存输入的值

时间:2015-09-24 08:49:50

标签: java jsp servlets

我有一个jsp页面,其中包含用户名&密码字段。对于这两个字段,我在servlet中进行服务器端验证。我将错误消息从servlet传递给jsp。现在我想要的是当jsp页面上显示错误消息的同时,只有字段应该包含我输入的值,除了错误消息字段。它不应该清除除错误消息字段之外的值。

UserLogin.java

String loginsubmit = request.getParameter("loginsubmit");
String username = request.getParameter("username");
String password = request.getParameter("password");

if(loginsubmit!=null){
if(username==null || username.isEmpty()){
  errors.put("username", "Please Enter User Name");
    }
if(password == null || password.isEmpty()){
  errors.put("password", "Please Enter Password");
   }
if(errors.size()>0)
   {
    request.setAttribute("map",errors);
    RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
    rd.forward(request, response);
   }else{
         int success_flag = fr.successFlag(username,password);
         HttpSession ses=request.getSession();
         String link = "<font color='red'>User Name Does Not Exist Please</font> <a href=\"NewUserForm.jsp\"><b><font color='#00ec00'>Create New Account</font></b></a>";
         String resp=(success_flag==1?"<font color='#00ec00' size='5'>Login Successful</font>":link);
         ses.setAttribute("resp_msg",resp);
      if(success_flag==1){
          response.sendRedirect("UserDetails.jsp");
            }
          else{
            response.sendRedirect("index.jsp");
               }
           }
     }





<head>
    <link rel="stylesheet" type="text/css" href="css/button.css">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>

</head>
 <%!
    Map<String,String> errors=null;
%>
<body onload="onloadfunction();">
      <%

      if(request.getAttribute("map")!=null){
       errors=(Map<String,String>)request.getAttribute("map");
       out.println(errors.get("username"));
       request.removeAttribute("map");
      }
    %>

<form method="post" action="./UserLogin" autocomplete="off">
        <table align="center">
            <tr>
                <td>
                     <b><font color="#00ec00">Enter Username and Password</font></b>
                </td>
            </tr>
            <tr>
                <td>User Name</td>
                <td>:</td>
                <td><input type="text" name="username" id="username" placeholder="User Name"/></td>
                <td style="color:red"><%=(errors!=null?(errors.get("username")!=null?errors.get("username"):""):"")%></td>
            </tr>
            <tr>
            <td>Password</td>
                <td>:</td>
                <td><input type="password" name="password" id="password" placeholder="Password"/></td>
                <td style="color:red"><%=(errors!=null?(errors.get("password")!=null?errors.get("password"):""):"")%></td>
            </tr>
            <tr>
                <td colspan="3" style="text-align: center">
                    <input type="submit" value="Sign in" name="loginsubmit" id="submit"/>
                </td>
            </tr>
            <tr>
                <td colspan="3" style="text-align: center">
                    <input type="button" value="Create Account" onclick="document.location.href='NewUserForm.jsp';" id="newuser"/>
                </td>
            </tr>
        </table>
    </form>
</body>

1 个答案:

答案 0 :(得分:-2)

我正在为Username.Similar代码编写代码。您也可以为密码编写。

<td>User Name</td>
<td>:</td>

    <%if(request.getParameter("username")==null){ %>>
    <td><input type="text" name="username" id="username" placeholder="User Name"/></td>
    <%}
    else{ %>
    <td><input type="text" name="username" id="username" value=<%=request.getParameter("username") %>> placeholder="User Name"/></td>
    <%}%>