我知道以前的问题已经被问过,找不到合适的解决方案......
我正在尝试创建一个登录页面。我已经有了一个有效的注册页面,但我的登录页面显示了这个错误:https://imgur.com/a/dOGmG
.jsp工作正常 - 我的登录页面出现,显示用户名和密码的文本框,以及登录按钮。但是当我输入凭证(正确和错误)时,我得到上面的imgur中显示的错误。
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Registration</display-name>
<servlet>
<servlet-name>reg</servlet-name>
<servlet-class>jdbc.Registration</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>reg</servlet-name>
<url-pattern>/regServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>jdbc.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
Login.java(Servlet)
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
public Login() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String name = request.getParameter("user");
String password = request.getParameter("password");
String dbName = null;
String dbPassword = null;
String sql = "select * from users where username=? and password=?";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root","");
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
PrintWriter out = response.getWriter();
while(rs.next()) {
dbName = rs.getString("username");
dbPassword = rs.getString("password");
}
if (name.equals(dbName)&&password.equals(dbPassword)) {
out.println("You have successfully Logged in!");
}
else {
//response.sendRedirect("login.jsp");
RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
rd.include(request, response);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
的Login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<div align="center">
<form action="Login" method="post">
Username: <input type="text" name="user" required="required">
Password: <input type="password" name="password" required="required">
<input type="submit" value="LOGIN">
</form>
</div>
</body>
MySQL数据库
数据库名称是“员工” 表名是“用户”