我创建了一个登录表单,当我使用正确的用户名和密码登录时,它工作正常。 当我输入错误的用户名或密码时,我想弹出错误消息或错误页面。 这意味着控制器会将给定的用户名与数据库中的所有用户名进行比较,如果找不到给定的用户名,则需要显示错误消息或错误页面。
这可能在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>
/**
* @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扩展了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中创建简单的错误消息?但我需要重新加载页面。
谢谢。
答案 0 :(得分:0)
如果您要重新加载部分页面,则应使用java脚本以便通过ajax发布您的用户名和密码,如果它们不匹配,您可以显示错误消息。
另一方面,你可以使用jsp页面来验证服务器端的用户名和密码,如果它们不匹配,你可以显示错误信息,但你必须再次加载整个页面。
我建议您使用第一个Ajax
。它更快。
答案 1 :(得分:0)
这绝对可以通过JavaScript完成。控制器应该将用户路由到错误页面,或者您需要使用validateForm()函数来使用警报功能。即。
if (!valid) {
alert("The username and/or password are invalid. Please try again");
}