Username and password validation from database to jsp view page
You can use a <span>
element where you show your error message that you get from your servlet request
, here's the JSP page:
<form id="loginform" class="form-horizontal" name="myForm" method="POST" action="ValidateLoginServlet2.do" onSubmit="return validateLogin()">
<input type="text" class="form-control" name="uname" placeholder="username">
<input id="login-password" type="password" class="form-control" name="pwd" placeholder="password">
<input type="submit" value="Login" href="#" class="btn btn-success" />
<span style="color:red;">${errMsg}</span>
</form>
And in your servlet you set an error message in your else statment like this:
if(result.next()) {
response.sendRedirect("LoginSuccessful.jsp");
}else{
request.setAttribute("errMsg", "username and password are incorrect");
// The following will keep you in the login page
RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
rd.forward(request, response);
}
And to prevent showing the same error the next login in your if block where the login is successful you can reset the ErrMsg
like this:
request.setAttribute("errMsg", "");
kittu
Worked on different stack of technologies with 6+ years of experience such as: Front-end stack: Javascript, Angular 7/8, HTML5/CSS3, Bootstrap Back-end stack: NodeJs, MongoDb with mongoose, AWS, RabbitMQ Tools: Git, VS Code, Jenkins Passionate about working on enterprise or product based companies with react, node and mongo tech stack
Updated on June 30, 2020Comments
-
kittu almost 4 years
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.
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?
Below is my Login form:
<form id="loginform" class="form-horizontal" name="myForm" method="POST" action="ValidateLoginServlet2.do" onSubmit="return validateLogin()"> <input type="text" class="form-control" name="uname" placeholder="username"> <input id="login-password" type="password" class="form-control" name="pwd" placeholder="password"> <input type="submit" value="Login" href="#" class="btn btn-success" /> </form>
And my Validate login servlet:
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(); } }
-
Shotgun Ninja almost 9 yearsI really hope you're encrypting those passwords before they get stored in the database or compared against DB values...
-
kittu almost 9 yearsDo they have to be encrypted? If so, then how?. Could you please give me a hint or a link about it. Thank you
-
-
kittu almost 9 yearsWhat if cookies are disabled in the client browser?
-
kittu almost 9 yearsThe else part is getting re-directed to login page as you have mentioned, however
<c:out value="${errMsg}"/>
error message is not being displayed on the view page -
kittu almost 9 yearsI tried as you said and its not working :/ where does the second part of code goes? It has to be in script tag right?
-
cнŝdk almost 9 years@kittu Well , simply try
${errMsg}
without<c:out>
like this:<span style="color:red;">${errMsg}</span>
-
Keval almost 9 years@kittu yes It has to be in script tag, please check the answer now