0

I created a login form, when i login using correct username and password it works fine. I want to pop up an error message or error page when i enter wrong username or password. That means the controller will compare the given username with all the usernames in database and if don't find the given username, then it needs to display an error message or error page.

Is this possible in HTML or JavaScript?

<html>
<body>
<form action="search" onsubmit="return validateForm()">
<table>
<tr><td>Username</td>
<td><input type=text name=LoginId /></td></tr>
<tr><td>Password</td>
<td><input type=password name=LoginPassword /> </td></tr>
<tr><td colspan=2>
<center>
<input type=submit value=SignIn /><br>If you forgot your password, <a href="ResetPassword"> Reset </a>your password.</center></td></tr>
</table>
</form>
</body>
</html>

SearchServlet.java

/**
 * @see HttpServlet#HttpServlet()
 */
public SearchServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    CustomerDAO customerDAO = new CustomerDAO();
    String username = request.getParameter("LoginId");
    String password = request.getParameter("LoginPassword");
    Login login = customerDAO.getLoginByName(username, password);


    PrintWriter out = response.getWriter();
    out.println("<html><body>");
    out.println("<center><h1>User Information</h1></center>");
    out.println("<center><table border=0x>");
    out.println("<tr>");
    out.println("<td>Login Id : </td><td>"+login.getLoginId()+"</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>City : </td><td>"+login.getCity()+"</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>State : </td><td>"+login.getState()+"</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>Cell Number : </td><td>"+login.getCellnumber()+"</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>Email :</td><td>"+login.getEmail()+"</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>Address :</td><td>"+login.getAddress()+"</td>");
    out.println("</tr>");
    out.println("<tr>");
    out.println("<td>ZipCode : </td><td>"+login.getZipcode()+"</td>");
    out.println("</tr>");
    out.println("</table></center>");
    out.println("<p>If these details are correct press continue or to change your information press update.</p>");
    out.println("<form action='Continue.jsp'>");
    out.println("<center><input type=submit value=continue></center>");
    out.println("</form>");
    out.println("<form action='search'>");
    out.println("<center><a href=Update?City="+login.getCity()+"&State="+login.getState()+"&PhoneNumber="+login.getCellnumber()+"&Email="+login.getEmail()+"&Address="+login.getAddress()+"&ZipCode="+login.getZipcode()+"> update </a></center>");          
    out.println("</form>");
    out.println("</body></html>");


    Login l = new Login();
    l.setLoginId(request.getParameter("LoginId"));

    if(l!=null) {
        HttpSession session = request.getSession();
        session.setAttribute("l", l);
        }

}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub



}

}

CustomerDAO.java

public class CustomerDAO extends BaseDAO{

public Login getLoginByName(String username, String password) {
    Login login = null;
    try {
        BaseDAO baseDAO = new BaseDAO();
        Connection c =baseDAO.getConnection();
//      String query = "select * from test.Login where LoginId=? && LoginPassword=?";
        String query = "select * from test.Customer where LoginId=? && LoginPassword=?";
        PreparedStatement ps = c.prepareStatement(query);
        ps.setString(1, username);
        ps.setString(2, password);
        ResultSet rs = ps.executeQuery();

        while(rs.next()) {
            login = new Login();
            login.setLoginId(rs.getString("LoginId"));
            login.setCity(rs.getString("City"));
            login.setState(rs.getString("State"));
            login.setCellnumber(rs.getString("PhoneNumber"));
            login.setEmail(rs.getString("Email"));
            login.setAddress(rs.getString("Address"));
            login.setZipcode(rs.getInt("ZipCode"));
            System.out.println();
        }
        c.close();
    }catch(Exception e) {
        System.err.println("Username or Password you enterd is incorrect.");
    }
    return login;
}

Is there a way to create simple error message in the servlet? but i need to reload the page again.

Thank You.

Beryllium
  • 12,808
  • 10
  • 56
  • 86
sapan
  • 259
  • 3
  • 6
  • 16

2 Answers2

0

If you are going to reload part of your page you should use java script in order to post your username and password by ajax and if they are not matched you can show error message.

on the other hand, you can use jsp page to verify username and password in server side and if they are not matched you can show error message but you have to load whole page again.

I recommend you using the first one Ajax. it's faster.

Super Hornet
  • 2,839
  • 5
  • 27
  • 55
0

This would definitely be done via JavaScript. Either the controller should route the user to an error page, or you would need your validateForm() function to use an alert function for something. i.e.

if (!valid) {
    alert("The username and/or password are invalid. Please try again");
}
EmptyArsenal
  • 7,314
  • 4
  • 33
  • 56
  • sorry, it didn't work out. We need to compare the given username and password with the content in the table and then need to display the error, right? How to do that. You showed me a condition if(!valid) {} but the controller don't know what field is to be compare to valid, right? So, can u please clarify my doubt. – sapan Sep 23 '13 at 13:53
  • can u please help me to fix this issue. I think using javascript is much more efficient than any thing else to display the error message. – sapan Sep 23 '13 at 21:42
  • Oops, had an error in the last comment (see corrected code). I'm not a Java developer, so I don't know exactly how you would implement that in Java. However, perhaps if you tried sending this to the user: `` Have it sent to user from inside your catch statement – EmptyArsenal Sep 24 '13 at 02:56
  • Even i don't know because i am also learner. :-) I don't know javascript. – sapan Sep 24 '13 at 03:07