0

我创建了一个登录表单,当我使用正确的用户名和密码登录时,它工作正常。当我输入错误的用户名或密码时,我想弹出错误消息或错误页面。这意味着控制器会将给定的用户名与数据库中的所有用户名进行比较,如果没有找到给定的用户名,则需要显示错误消息或错误页面。

这在 HTML 或 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

公共类 CustomerDAO 扩展 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;
}

有没有办法在 servlet 中创建简单的错误消息?但我需要重新加载页面。

谢谢你。

4

2 回答 2

0

如果您要重新加载页面的一部分,您应该使用 java 脚本以通过 ajax 发布您的用户名和密码,如果它们不匹配,您可以显示错误消息。

另一方面,您可以使用 jsp 页面在服务器端验证用户名和密码,如果它们不匹配,您可以显示错误消息,但您必须再次加载整个页面。

我建议你使用第一个Ajax。它更快。

于 2013-09-23T05:14:56.670 回答
0

这肯定会通过 JavaScript 完成。控制器应该将用户路由到错误页面,或者您需要您的 validateForm() 函数来为某些事情使用警报函数。IE

if (!valid) {
    alert("The username and/or password are invalid. Please try again");
}
于 2013-09-23T05:17:08.190 回答