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