我有一个MySQL数据库,我在其中创建了一个用户标识“mads”和密码“mads”。如果用户存在于数据库中,我想通过JSP站点和servlet进行测试。我认为我的代码是正确的,但我一直得到错误的异常...我的用户无效。所以我想我的代码不正确: - / 与我的数据库的连接是成功的。在不知情的情况下,我有一种感觉,我的servlet会查看数据库中的最后一列,而不是第一列?我不知道这与它有什么关系。任何人都能看出它的错误吗?
最诚挚的问候 MADS 我的JSP页面如下所示:
<title>Validation</title>
</head>
<body>
<br><br><br>
<center>
<h1>Please enter user name and password</h1>
<form name="frm" action="LoginValidation" method="post">
<input type="text" name="user">
<input type ="password" name="pass">
<input type="submit" value="Check">
</form>
</center>
</body>
和我的Servlet:
package jsp;
import java.io.*;
//import java.util.*;
import java.sql.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.*;
@WebServlet(urlPatterns = {"/LoginValidation"})
public class Validation extends HttpServlet {
private static final long serialVersionUID = 1L;
private ServletConfig config;
public void init (ServletConfig config)
throws ServletException{
this.config = config;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
PrintWriter out = response.getWriter();
String connectionURL = "jdbc:mysql://localhost/dblogin";
Connection connection = null;
ResultSet rs;
String userid = new String("");
String password = new String("");
response.setContentType("text/html");
try {
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "");
//Add the data into the database
String sql = "SELECT userid, password FROM login";
Statement s = connection.createStatement();
s.executeQuery(sql);
rs = s.getResultSet();
while(rs.next()) {
userid = rs.getString("userid");
password = rs.getString("password");
}
rs.close();
s.close();
} catch(Exception e) {
System.out.println("Exception is: " + e);
}
if(userid.equals(request.getParameter("userid")) && password.equals(request.getParameter("password"))) {
out.println("The user is valid");
}
else {
out.println("You are not valid");
}
}
}
答案 0 :(得分:1)
在此代码中,您将用户输入值与表的最后一行数据进行比较....在while循环内写入比较代码以比较所有值....并在任何值与数据库值匹配时进行重定向... .....
package jsp;
import java.io.*;
//import java.util.*;
import java.sql.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.*;
@WebServlet(urlPatterns = {"/LoginValidation"})
public class Validation extends HttpServlet {
private static final long serialVersionUID = 1L;
private ServletConfig config;
public void init (ServletConfig config)
throws ServletException{
this.config = config;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
PrintWriter out = response.getWriter();
String connectionURL = "jdbc:mysql://localhost/dblogin";
Connection connection = null;
ResultSet rs;
String userid = new String("");
String password = new String("");
response.setContentType("text/html");
try {
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL, "root", "");
//Add the data into the database
String sql = "SELECT userid, password FROM login";
Statement s = connection.createStatement();
s.executeQuery(sql);
rs = s.getResultSet();
while(rs.next()) {
userid = rs.getString("userid");
password = rs.getString("password");
if(userid.equals(request.getParameter("userid")) && password.equals(request.getParameter("password")))
{
out.println("The user is valid");
//Write exit program code or rediraction code here
}
}
rs.close();
s.close();
} catch(Exception e) {
System.out.println("Exception is: " + e);
}
}
}
//我认为这不是一种有效的登录方式........但我只是在你的类型登录中给你问题解决方案.........