我有一个 MySQL 数据库,我在其中创建了用户 ID“mads”和密码“mads”。我想通过 JSP 站点和 servlet 测试用户是否存在于数据库中。我认为我的代码是正确的,但我不断收到错误的异常......我的用户无效。所以我猜我的代码不正确:-/ 与我的数据库的连接成功。在不知情的情况下,我有一种感觉,我的 servlet 查看数据库中的最后一列,而不是第一列?我不知道这是否与此有关。有人能看出哪里出了问题吗?
致以诚挚的问候 麦兹 我的 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");
}
}
}
最佳答案
在此代码中,您将用户输入值与表的最后一行数据进行比较...在 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);
}
}
}
//我认为这不是有效的登录方式......但我只是在你的类型登录中为你提供了问题解决方案......
关于java - 登录在我的 servlet 程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21645164/