非常奈奈思 2023-06-30 00:29 采纳率: 100%
浏览 42
已结题

java连接数据库,用户的密码必须加密存储在数据库中

在以下代码中进行注册操作时,连接数据库对比注册邮箱和密码,如果数据库已存在相同邮箱则提示“邮箱已注册”,否则将新的用户信息写入webUser表中,这个功能一直都实现不了,代码菜鸟急需大佬求助
登录页面(login.jsp):
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<style type="text/css">
body {
    background-image: url("11.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    background-color: #f2f2f2;
    font-family: Arial, sans-serif;
}

h1 {
    text-align: center;
    color: #333;
}

form {
    margin: 0 auto;
    width: 400px;
    padding: 20px;
    background-color: rgba(255, 255, 255, 0.5);
    border-radius: 5px;
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}

label, input, button, select, option {
    display: block;
    margin-bottom: 10px;
}

label {
    font-weight: bold;
}

input[type="email"], input[type="password"] {
    width: 100%;
    padding: 5px;
    border-radius: 3px;
    border: none;
    box-shadow: inset 0px 1px 3px rgba(0, 0, 0, 0.1);
}

input[type="submit"] {
    background-color: #4CAF50;
    color: #fff;
    padding: 10px;
    border-radius: 5px;
    border: none;
    cursor: pointer;
    transition: background-color ease-in-out 0.3s;
    box-shadow: inset 0px 1px rgba(255, 255, 255, 0.6), inset 0px 1px
        rgba(255, 255, 255, 0.6);
}

input[type="submit"]:hover {
    background-color: #3e8e41
}
</style>
</head>
<body>
    <h1>教务系统登录</h1>
    <form action="login" method="post">
        <label for="email">登录邮箱:</label> <input type="email" id="email"
            name="email" required><br> <label for="password">密码:</label>
        <input type="password" id="password" name="password" required><br>
        <input type="submit" value="登录"> <a href="register.jsp">注册</a>
    </form>
    <script>
        function login() {
            var email = document.getElementById("email").value;
            var password = document.getElementById("password").value;
            alert("登录成功");
            window.location.href = "data.jsp";
        }
    </script>
</body>
</html>




注册页面(register.jsp):
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
<style>
body {
    background-image: url("11.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    background-color: #f2f2f2;
    font-family: Arial, sans-serif;
}

.register-container {
    text-align: center; /* 添加此属性使文本居中显示 */
    border: 1px solid gray;
    padding: 20px;
    background-color: rgba(255, 255, 255, 0.5);
    box-shadow: 0px 0px 5px gray;
    margin: 0 auto;
    margin-top: 100px;
    max-width: 400px;
}

.register-container h2 {
    margin: 0 auto;
}

}
.input-group {
    display: inline-block;
    margin-bottom: 15px;
}

.input-group label {
    font-weight: bold;
    margin-bottom: 5px;
}

.input-group input {
    width: 200px;
    height: 30px;
    padding: 5px;
    font-size: 16px;
    border: 1px solid gray; /* 添加输入框的边框 */
    border-radius: 5px; /* 设置输入框的圆角 */
    margin-bottom: 10px; /* 添加输入框之间的间距 */
}

}
.register-btn {
    padding: 10px;
    background-color: red; /* 将背景颜色设置为红色 */
    color: white;
    border: none;
    cursor: pointer;
    font-size: 16px;
}

</style>
</head>
<body>
    <div class="register-container">
        <h2>注册</h2>
        <div class="input-group">
            <label for="userName">用户名:</label> <input type="text" id="userName"
                required>
        </div>
        <div class="input-group">
            <label for="email">邮箱:</label> <input type="email" id="email"
                required>
        </div>
        <div class="input-group">
            <label for="password">密码:</label> <input type="password"
                id="password" required>
        </div>
        <button class="register-btn" onclick="register()">注册</button>
    </div>

    <script>
        function register() {
            var userName = document.getElementById("userName").value;
            var email = document.getElementById("email").value;
            var password = document.getElementById("password").value;

            // 示例代码:弹出注册成功的窗口,并跳转到登录页面
            alert("注册成功!");
            window.location.href = "login.jsp?email=" + email + "&password="
                    + password; // 登录页面的路径,并传递邮箱和密码参数
        }
    </script>
</body>
</html>


数据展示页面(data.jsp):
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%!boolean userIsLoggedIn = true;%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>数据展示页面</title>
<style type="text/css">
h1 {
    text-align: center;
}

table {
    margin: 0 auto;
}
</style>
</head>
<body>
    <h1>学生信息表</h1>
    <table border="1">
        <tr>
            <th>系号</th>
            <th>学号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>出生日期</th>
            <th>地址</th>
            <th>民族</th>
            <th>班级</th>
            <th>专业</th>
        </tr>
        <%
        HttpSession localSession = request.getSession();
        String email = (String) session.getAttribute("email");
        String password = (String) session.getAttribute("password");
        // 使用获取到的邮箱和密码进行数据展示或其他操作
        %>
        <%
        try {
            // 在此处添加验证用户是否已登录的代码
            if (userIsLoggedIn) { // 替换为你自己的判断条件
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jwxt", "root", "1234");
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * FROM student");
                while (rs.next()) {
        %>
        <tr>
            <td><%=rs.getString("系号")%></td>
            <td><%=rs.getString("学号")%></td>
            <td><%=rs.getString("姓名")%></td>
            <td><%=rs.getString("性别")%></td>
            <td><%=rs.getString("出生日期")%></td>
            <td><%=rs.getString("地址")%></td>
            <td><%=rs.getString("民族")%></td>
            <td><%=rs.getString("班级")%></td>
            <td><%=rs.getString("专业")%></td>
        </tr>
        <%
        }
        rs.close();
        stmt.close();
        con.close();
        } else {
        // 用户未登录,显示登录提示或跳转到登录页面
        response.sendRedirect("login.jsp"); // 替换为你自己的登录页面路径
        }
        } catch (Exception e) {
        e.printStackTrace();
        }
        %>
    </table>
</body>
</html>




登录操作(Login.java):
package qimo;
import java.io.IOException;
import java.sql.*;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

@WebServlet("/login")
public class Login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String email = request.getParameter("email");
        String password = request.getParameter("password");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jwxt", "root", "1234");
            PreparedStatement stmt = con.prepareStatement("SELECT * FROM webUser WHERE email=? AND password=?");
            stmt.setString(1, email);
            stmt.setString(2, password);
            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {
                HttpSession session = request.getSession();
                session.setAttribute("email", email);
                session.setAttribute("password", password);

                response.sendRedirect("data.jsp");
            } else {
                response.sendRedirect("login.jsp");
            }

            rs.close();
            stmt.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




注册操作(Register.java):
package qimo;

import java.io.IOException;
import java.sql.*;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

@WebServlet("/register")
public class Register extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String email = request.getParameter("email");
        String password = request.getParameter("password");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jwxt", "root", "1234");
            
            // 检查邮箱是否已注册
            PreparedStatement stmt = con.prepareStatement("SELECT * FROM webUser WHERE email = ?");
            stmt.setString(1, email);
            ResultSet rs = stmt.executeQuery();
            
            if (rs.next()) {
                // 邮箱已注册
                response.sendRedirect("register.jsp?error=邮箱已注册");
            } else {
                // 将新用户信息写入数据库
                stmt = con.prepareStatement("INSERT INTO webUser (username, email, password) VALUES (?, ?, ?)");
                stmt.setString(1, username);
                stmt.setString(2, email);
                stmt.setString(3, password);

                int rowsAffected = stmt.executeUpdate();

                if (rowsAffected > 0) {
                    HttpSession session = request.getSession();
                    session.setAttribute("email", email);
                    session.setAttribute("password", password);

                    response.sendRedirect("login.jsp");
                } else {
                    response.sendRedirect("register.jsp?error=注册失败,请重试");
                }
            }

            rs.close();
            stmt.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}





  • 写回答

3条回答 默认 最新

  • Huazie 优质创作者: 编程框架技术领域 2023-06-30 09:02
    关注

    你现在注册是有啥问题? 啥叫没有实现功能?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月30日
  • 已采纳回答 6月30日
  • 创建了问题 6月30日