java - 将 MySql 与 Servlet 连接用于登录页面

标签 java mysql jsp tomcat servlets

如何将MySql 数据库与servlet 连接到登录页面? 现在在 servlet 中我有预定义用户名 (gogikole) 和密码 (1234) 的代码

String username = request.getParameter("username");
            String password = request.getParameter("password");

            if (username.equals("gogikole") && password.equals("1234")) {
                response.sendRedirect("mainMenu.jsp");
                return;
            }

但是如何检查数据库中其他用户的用户名和密码是否正确? 例如用户名:konsta,密码:4321

我有这样的代码:

登录Servlet

package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.*;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username.equals("gogikole") && password.equals("1234")) {
            response.sendRedirect("mainMenu.jsp");
            return;
        }
    }
}

登录.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 page</title>
</head>
<body>
<form method="post" action="LoginServlet">
    <table align="center">
        <tr>
            <td>User name</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Login"></td>
        </tr>
    </table>
</form>
</body>
</html>

enter image description here

此外,我已经将 mysqlconnector 添加到 Eclipse。

最佳答案

首先,您应该使用 MysqlConnection 连接到 mysql。

然后,使用 sql 创建一条语句,例如 SELECT * FROM users WHERE username = :username AND password = :password。然后执行它。

你会得到一个包含匹配用户结果的结果集,当然,如果用户名或密码错误,结果集为空。

解决问题的关键是你应该查询 username.equals("gogikole") && password.equals("1234") 的数据库

例子:

final String DB_URL = "localhost:3306/db";  //Mysql Address
final String USER = "root";  //mysql username
final String PASS = "123456";//mysql password
final String SQL = "SELECT * FROM users WHERE username = ? AND password = ?";  //SQL
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection conn = null;  //mysql connection
Statement stmt = null;   //mysql statement
Class.forName("com.mysql.jdbc.Driver");  //register driver
conn = DriverManager.getConnection(DB_URL,USER,PASS); //get connection
stmt = conn.prepareStatement(SQL); //create statement
//init param
stmt.setString(1,username);
stmt.setString(2,password);
ResultSet rs = stmt.executeQuery();  //execute query
if(rs.first()){ //finded
    response.sendRedirect("mainMenu.jsp");
    return;
}

关于java - 将 MySql 与 Servlet 连接用于登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50244338/

相关文章:

mysql - 根据ID更新表中的列并一次更新一行

mysql - 是否有示例 MySQL 数据库?

java - 为什么我的 servlet request.getParameter 返回 null?

java - 如何将 jsp 中检索到的服务器响应映射到 iFrame

java - 在连续更新的批处理中运行时 couchdb 查询超时

java - 为什么cucumber会找 "runTest.java"跑测试?

php - mysql子查询返回多行

java - 从 jsp 向 servlet 传递值,但 servlet 不可用

java - 相等的对象必须具有相等的哈希码?

java - 使用枚举的数组超出绑定(bind)异常