title: Username and password validation from database to jsp view page
body: <p>If the username and password retrieved from database are incorrect then I would like to show error on jsp page itself instead of re-directing to another page. </p>
  
  <p>Right now I am showing a message from the validation servlet if the username and passwords are invalid. How do I show a message on front end using javascript or anyother tool to jsp view?</p>
  
  <p>Below is my Login form:</p>
  
  <pre><code>&lt;form id="loginform" class="form-horizontal" name="myForm" method="POST" action="ValidateLoginServlet2.do" onSubmit="return validateLogin()"&gt;
      &lt;input type="text" class="form-control" name="uname" placeholder="username"&gt;                                        
      &lt;input id="login-password" type="password" class="form-control" name="pwd" placeholder="password"&gt;
      &lt;input type="submit" value="Login" href="#" class="btn btn-success" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And my Validate login servlet:</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
  //        processRequest(request, response);
          PrintWriter out = response.getWriter();
  
          String username = request.getParameter("uname");
          String password = request.getParameter("pwd");
          System.out.println(username);
          System.out.println(password);
  
          try
          {
              Connection con = OracleDBConnection.getConnection();
              PreparedStatement statement = con.prepareStatement("select firstname, password from registration where firstname =? and password=?");
              statement.setString(1, username);
              statement.setString(2, password);
              ResultSet result = statement.executeQuery();
              if(result.next()){
                  response.sendRedirect("LoginSuccessful.jsp");
              }else{
                  out.println("username and password are incorrect");
              }
          }catch(Exception e){
              System.out.println("DB related Error");
              e.printStackTrace();
          }   
      }
  </code></pre>