1

I have an HTML form for user login on a JSP document and I send the input of the user to a Servlet, check the password against a database then redirect the user to a new page if it's correct or show an error message on the page "Incorrect Password" of incorrect. I want the error message to appear on the same page as the form just below the Password input field. I have tried the code below but it still redirects me to a new page where it displays only the "Incorrect Password" message. Is there any other way on how to do it? Thank you in advance.

//SERVLET CODE

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

            //Some code to check if the password matches the one in the DB and redirects to another page.

                } else {

                String text = "Incorrect Password!";

               response.setContentType("text/plain"); 
               response.setCharacterEncoding("UTF-8"); 
               response.getWriter().write(text); 


                }

            }

        }

//JSP PAGE

<!DOCTYPE html>
<html>
<head>
 <script>
$('#sButton').click(function() {
       $.post('UserServlet', function(responseText) {
              $('#msg').text(responseText);   
           });
     });
 </script>
<title>User Log in</title>
<%@ include file="PageHeader.html" %>
</head>
<body>
<form action = "SomeServlet" method="POST">
<h3 >Enter detailsto Sign In</h3><br>
Email: <input type="text" name="email" required><br>
Password: <input type="password" name="password" required><br>
<input type="submit" name="submit" value="Sign in">
<div style="color:red" id="msg"></div>
</form>
</body>
</html>

The error message should appear on this same page as I understand but it just opens a new page showing only the message and not the html form.

user1234567
  • 37
  • 1
  • 10

0 Answers0