GDPU JavaWeb 网站的登录注册页面

当有了一定的三件套基础跟数据库基础,你将不再是后端小白,可以尝试做一个网站的登录页面了。

条件查询

 1.编写一个网页,实现根据输入学生姓名的模糊查询,如果查找不到就显示“查无此人”。

这题是不是似曾相识,在这里,差不多的。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>查询</h1>
<form method="post">

    <label>查找的姓名:</label>
    <input type="text" name="name"><br>
    <input type="submit" value="查询">
</form>

<table border="1">

    <%
        request.setCharacterEncoding("UTF-8");
        String stuname = request.getParameter("name");

        // 数据库连接信息
        String url = "jdbc:mysql://localhost:3306/dbjsp?serverTimezone=UTC";
        String user = "root";
        String password = "123456";

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, user, password);

            String sql = "SELECT * FROM student1  WHERE name like ?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, "%"+stuname+"%");
            ResultSet rs = pst.executeQuery();
            // 输出查询结果
            boolean found = false;
            while (rs.next()) {
                found = true;
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String sex = rs.getString("sex");

    %>
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>

    </tr>
    <tr>
        <td><%= id %></td>
        <td><%= name %></td>
        <td><%= sex %></td>

    </tr>
    <%
        }
        // 如果没有找到匹配的学生信息,则输出消息
        if (!found) {
    %>
    <tr>
        查无此人
    </tr>
    <%
            }
            // 关闭资源
            if (rs != null)
                rs.close();
            if (pst != null)
                pst.close();
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            out.println("查询出错:" + e.getMessage());
        }
    %>
</table>
</body>
</html>

登陆注册页面 

2.实现学生注册和登录功能,输入学生学号(stuId)和密码(stuPwd),如果学号不存在,跳转到注册界面,完成注册功能;如果学号存在,密码匹配显示“登录成功”,否则显示“登录失败”。注:要求用PreparedStatement实现。

安排。

登录页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>学生登录</title>
    <style>
        .center{
            text-align: center;
        }
        .login-btn {
            background-color: #4CAF50; /* 添加你想要的颜色 */
            color: white;
            padding: 10px 20px;
            border: none;
            cursor: pointer;
            border-radius: 5px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin-top: 10px; /* 如果需要,可以调整按钮与输入框之间的间距 */
        }
    </style>
</head>
<body>
<div class="center" >

<h2>学生登录</h2>
<form  method="post">
    学号:<input type="text" name="stuId"><br>
    密码:<input type="password" name="stuPwd"><br>
    <input type="submit" value="登录"  class="login-btn">
</form>
</div>
                <%
                    try {
                        request.setCharacterEncoding("UTF-8");
                        String stuId = request.getParameter("stuId");
                        String stuPwd = request.getParameter("stuPwd");
                        // 数据库连接信息
                        if (stuId != null && stuPwd != null) {
                            String url = "jdbc:mysql://localhost:3306/dbjsp?serverTimezone=UTC";
                            String user = "root";
                            String password = "123456";
                            Class.forName("com.mysql.cj.jdbc.Driver");
                            Connection conn = DriverManager.getConnection(url, user, password);

                            String sql = "SELECT * FROM student2 WHERE id = ?";
                            PreparedStatement pst = conn.prepareStatement(sql);
                            pst.setString(1, stuId);
                            ResultSet rs = pst.executeQuery();
                            // 输出查询结果
                            if (rs.next()) {
                                // 如果学号存在,检查密码是否匹配
                                String dbPwd = rs.getString("psd");
                                if (dbPwd.equals(stuPwd)) {
                                    // 密码匹配,登录成功
                %>
                <script>
                    alert("登录成功");
                </script>
                <%
                } else {
                    // 密码不匹配,登录失败
                %>
                <script>
                    alert("登录失败");
                </script>
                <%
                                }
                            } else {
                                // 学号不存在,重定向到注册页面
                                response.sendRedirect("register.jsp");
                            }

                            // 关闭资源
                            if (rs != null)
                                rs.close();
                            if (pst != null)
                                pst.close();
                            if (conn != null)
                                conn.close();
                        }
                    } catch (Exception e) {
                        out.println(e.getMessage());
                    }
                %>
</body>
</html>

注册页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>学生注册</title>
    <style>
        .center{
            text-align: center;
        }
        .login-btn {
            background-color: #4CAF50; /* 添加你想要的颜色 */
            color: white;
            padding: 10px 20px;
            border: none;
            cursor: pointer;
            border-radius: 5px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin-top: 10px; /* 如果需要,可以调整按钮与输入框之间的间距 */
        }
    </style>
</head>
<body>
<div class="center" >

    <h2>学生注册</h2>
    <form  method="post">
        学号:<input type="text" name="stuId"><br>
        密码:<input type="password" name="stuPwd"><br>
        <input type="submit" value="注册"  class="login-btn">
    </form>
</div>
                <%
                    try {
                        request.setCharacterEncoding("UTF-8");
                        String stuId = request.getParameter("stuId");
                        String stuPwd = request.getParameter("stuPwd");

                        if (stuId != null && stuPwd != null) {
                        String url = "jdbc:mysql://localhost:3306/dbjsp?serverTimezone=UTC";
                        String user = "root";
                        String password = "123456";
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        Connection conn = DriverManager.getConnection(url, user, password);

                        String sql = "INSERT INTO student2 (id, psd) VALUES (?, ?)";
                        PreparedStatement pst = conn.prepareStatement(sql);
                        pst.setString(1, stuId);
                        pst.setString(2, stuPwd);
                        int rowsAffected = pst.executeUpdate();

                        if (rowsAffected > 0) {
                            // 插入成功
                %>
                <script>
                    alert("注册成功");
                </script>
                <%
                } else {
                    // 插入失败
                %>
                <script>
                    alert("注册失败");
                </script>
                <%
                        }

                        // 关闭资源
                        if (pst != null)
                            pst.close();
                        if (conn != null)
                            conn.close();
                    }
                    }catch (Exception e) {
                        out.println(e.getMessage());
                    }
                %>
</body>
</html>

注:如果只为作业,可以不用加样式。

没有这个,跳到注册页面。

可以看到数据库新的记录 

这时,再用这个账号密码去登录也可登录成功了。这样就可以实现一个简单的网站登录注册页面了。 

实验心得

最近在写剧本,有点多。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值