0

i have a jsp page which contains user name & password fields. For that two fields i am doing server side validation in servlet. And i am passing the error messages from servlet to jsp. Now what i want is when the error message is displayed on the jsp page at the same time only the fields should hold whatever the values i entered except error message field. It should not clear the values except error message fields.

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>
Asha
  • 115
  • 1
  • 13
  • I would recommend using some security frameworks like Spring-Security or Apache Shiro, rather then manually managing authentication and authorization related stuff. – We are Borg Sep 24 '15 at 08:51
  • 1
    You will have to set the value field manualy `` or use AJAX or use JS for checking for empty feilds, or use HTML5 : `` – user63762453 Sep 24 '15 at 08:54
  • Thank you soo much Nivedita its working – Asha Sep 24 '15 at 09:34

1 Answers1

-2

I am writing code for Username.Similar code You can write for password too.

<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>
    <%}%>
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35