当前位置: 首页 > article >正文

1月第四讲:Java Web学生自习管理系统

一、项目背景与需求分析

随着网络技术的不断发展和学校规模的扩大,学生自习管理系统的需求日益增加。传统的自习管理方式存在效率低下、资源浪费等问题,因此,开发一个智能化的学生自习管理系统显得尤为重要。该系统旨在提高自习室的利用率和管理效率,为学生提供方便快捷的自习预约服务,同时为管理员提供高效的资源管理工具。

系统的主要功能需求包括:

1.用户管理:管理员和学生的注册、登录。

2.自习室管理:自习室类型、座位信息的录入和查询。

3.座位预约:学生预约自习室座位、查看预约状态和历史记录。

4.管理员操作:管理员查看预约情况、管理资源分配。

二、技术选型与架构设计

1.技术选型:

  • 前端:HTML、CSS、JavaScript,用于创建用户界面。
  • 后端:Java(JDK 1.8),Servlet,JSP,JDBC,用于处理业务逻辑和数据库交互。
  • 数据库:MySQL,用于存储用户信息和自习室资源数据。
  • 服务器:Apache Tomcat,用于部署和运行Web应用。
  • 开发工具:IntelliJ IDEA 或 Eclipse,用于编写和调试代码。

2.架构设计:

三层架构:

  • 表示层:JSP/HTML 作为前台与用户交互,Servlet 用于控制跳转和调用业务逻辑层。
  • 业务逻辑层:处理业务逻辑,调用数据访问层。
  • 数据访问层:与数据库交互,封装数据库操作。

三、数据库设计

1.用户表(users):存储用户信息。

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    password VARCHAR(50) NOT NULL,
    role VARCHAR(10) NOT NULL CHECK (role IN ('student', 'admin'))
);

2.自习室表(study_rooms):存储自习室信息。

CREATE TABLE study_rooms (
    id INT AUTO_INCREMENT PRIMARY KEY,
    room_name VARCHAR(50) NOT NULL,
    capacity INT NOT NULL
);

3.座位表(seats):存储座位信息。

CREATE TABLE seats (
    id INT AUTO_INCREMENT PRIMARY KEY,
    room_id INT NOT NULL,
    status VARCHAR(10) NOT NULL CHECK (status IN ('available', 'booked')),
    FOREIGN KEY (room_id) REFERENCES study_rooms(id)
);

4.预约表(reservations):存储预约信息。

CREATE TABLE reservations (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    seat_id INT NOT NULL,
    start_time DATETIME NOT NULL,
    end_time DATETIME NOT NULL,
    FOREIGN KEY (user_id) REFERENCES users(id),
    FOREIGN KEY (seat_id) REFERENCES seats(id)
);

四、后端实现

1.数据库连接工具类(DBUtil.java)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
public class DBUtil {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/study_management";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "your_password";
    private static Connection connection = null;
 
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
 
    public static Connection getConnection() {
        return connection;
    }
 
    // 其他数据库操作方法(增删改查)
}

2.数据访问层(DAO)

  • UserDao.java

    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
     
    public class UserDao {
        public boolean registerUser(String username, String password, String role) {
            String sql = "INSERT INTO users (username, password, role) VALUES (?, ?, ?)";
            try (PreparedStatement pst = DBUtil.getConnection().prepareStatement(sql)) {
                pst.setString(1, username);
                pst.setString(2, password);
                pst.setString(3, role);
                return pst.executeUpdate() > 0;
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        }
     
        // 其他方法(登录、查询用户等)
    }
    
  • StudyRoomDao.java 和 SeatDao.java 以及 ReservationDao.java 的实现类似,包含各自的增删改查方法。

3.业务逻辑层(Service)

  • UserService.java

    public class UserService {
        public boolean register(String username, String password, String role) {
            return UserDao.registerUser(username, password, role);
        }
     
        // 其他方法(登录验证、查询用户信息等)
    }
    
  • StudyRoomService.javaSeatService.java 和 ReservationService.java 类似,包含各自的业务逻辑处理。

4. Servlet

  • RegisterServlet.java:

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.IOException;
     
    public class RegisterServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String role = request.getParameter("role");
     
            boolean isRegistered = UserService.register(username, password, role);
     
            if (isRegistered) {
                response.sendRedirect("login.jsp");
            } else {
                request.setAttribute("error", "Registration failed!");
                request.getRequestDispatcher("register.jsp").forward(request, response);
            }
        }
     
        // 其他方法(登录Servlet、预约Servlet等)
    }
    

5. JDBC工具类

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
public class JDBCUtils {
    private static final String URL = "jdbc:mysql://localhost:3306/StudyManagementSystem?useSSL=false&serverTimezone=UTC";
    private static final String USER = "root";
    private static final String PASSWORD = "your_password"; // 请替换为您的数据库密码
 
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }
}

6. Servlet示例:添加学生

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 java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
@WebServlet("/addStudent")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String major = request.getParameter("major");
        int grade = Integer.parseInt(request.getParameter("grade"));
        String phone = request.getParameter("phone");
 
        String sql = "INSERT INTO Student (name, major, grade, phone) VALUES (?, ?, ?, ?)";
 
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, name);
            pstmt.setString(2, major);
            pstmt.setInt(3, grade);
            pstmt.setString(4, phone);
            pstmt.executeUpdate();
 
            response.sendRedirect("students.jsp"); // 重定向到学生列表页面
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("error", "添加学生失败,请稍后再试!");
            request.getRequestDispatcher("addStudent.jsp").forward(request, response);
        }
    }
}

7. JSP页面示例:添加学生页面

<!-- addStudent.jsp -->
<!DOCTYPE html>
<html>
<head>
    <title>添加学生</title>
</head>
<body>
    <h2>添加学生</h2>
    <form action="addStudent" method="post">
        姓名: <input type="text" name="name" required><br>
        专业: <input type="text" name="major" required><br>
        年级: <input type="number" name="grade" required><br>
        电话: <input type="text" name="phone" required><br>
        <input type="submit" value="提交">
    </form>
    <c:if test="${not empty error}">
        <p style="color:red;">${error}</p>
    </c:if>
</body>
</html>

注意:为了使用JSP标签库(如<c:if>),您需要在JSP页面顶部添加以下指令:

jsp复制代码

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

并且,您需要在项目的WEB-INF/lib目录下添加JSTL库(如jstl-1.2.jar)。

相关文章:“

https://book.qq.com/book-search/%E5%90%8D%E4%BC%98%E9%A6%86%E7%BD%91%E3%80%9023Y4.com%E3%80%91?c31a
https://book.qq.com/book-search/%E6%B5%B7%E8%A7%92%E7%A4%BE%E5%8C%BA%E3%80%9023Y4.com%E3%80%91?c31b
https://book.qq.com/book-search/%E8%89%B3%E6%AF%8D%E7%BD%91%E8%BF%9B%E3%80%9023Y4.com%E3%80%91?c31c
https://book.qq.com/book-search/%E6%9E%9C%E5%86%BB%E4%BC%A0%E5%AA%92%E3%80%9023Y4.com%E3%80%91?c31d
https://book.qq.com/book-search/%E6%9E%9C%E5%86%BB%E4%BC%A0%E5%AA%92%E8%BF%9B23Y4.com%E7%9C%8B?c31e
https://book.qq.com/book-search/%E6%80%A7%E5%B7%B4%E5%85%8B%E8%BF%9B%E3%80%9023Y4.com%E3%80%91?c31f
https://book.qq.com/book-search/%E7%88%B1%E5%A8%81%E5%A5%B6%E7%BD%91%E3%80%9023Y4.com%E3%80%91?c31g
https://book.qq.com/book-search/%E7%A6%81%E6%BC%AB%E5%A4%A9%E5%A0%82%E3%80%9023Y4.com%E3%80%91?c31h
https://book.qq.com/book-search/%E6%92%B8%E6%92%B8%E7%A4%BE%E7%BD%91%E3%80%9023Y4.com%E3%80%91?c31i
https://book.qq.com/book-search/%E6%8A%96%E9%98%B4%E4%B8%8B%E8%BD%BD%E3%80%8A23Y4.com%E3%80%8B?c31i
https://book.qq.com/book-search/%E6%8A%96%E9%98%B4%E7%BD%91%E7%AB%99%E3%80%9023Y4.com%E3%80%91?c31k
https://book.qq.com/book-search/%E4%B9%85%E4%B9%85%E7%83%AD%E7%9C%8B%E3%80%9023Y4.com%E3%80%91?c31l
https://book.qq.com/book-search/%E4%B9%85%E4%B9%85%E7%83%AD%E8%BF%9B%E3%80%9023Y4.com%E3%80%91?c31m
https://book.qq.com/book-search/%E6%92%B8%E6%92%B8%E7%A4%BE%E8%BF%9B%E3%80%9023Y4.com%E3%80%91?c31n
https://book.qq.com/book-search/%E4%B8%8B%E8%BD%BD%E6%8A%96%E9%98%B4%E3%80%9023Y4.com%E3%80%91?c31o

https://book.qq.com/book-search/%E8%89%B3%E6%AF%8D%E5%9C%B0%E5%9D%80%E3%80%9023Y4.com%E3%80%91?c31p
https://book.qq.com/book-search/%E7%9C%8B%E8%89%B3%E6%AF%8D%E8%BF%9B%E3%80%9023Y4.com%E3%80%91?c31q
https://book.qq.com/book-search/%E8%89%B3%E6%AF%8D%E7%9C%8B%E8%BF%99%E3%80%9023Y4.com%E3%80%91?c31r
https://book.qq.com/book-search/%E8%89%B3%E6%AF%8D%E5%9C%B0%E5%9D%80%E8%BF%9B23Y4.com%E7%9C%8B?c31s
https://book.qq.com/book-search/%E8%89%B3%E6%AF%8D%E5%9C%B0%E5%9D%80%E6%9D%A523Y4.com%E7%9C%8B?c31t

五、前端实现

1.注册页面(register.jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Register</title>
</head>
<body>
    <h1>Register</h1>
    <form action="RegisterServlet" method="post">
        Username: <input type="text" name="username" required><br>
        Password: <input type="password" name="password" required><br>
        Role: <select name="role">
                <option value="student">Student</option>
                <option value="admin">Admin</option>
            </select><br>
        <button type="submit">Register</button>
    </form>
    <c:if test="${not empty error}">
        <p style="color:red">${error}</p>
    </c:if>
</body>
</html>

2.登录页面(login.jsp)

登录页面(login.jsp)** 和 其他页面(如自习室管理页面、座位预约页面等)类似,通过表单提交数据到相应的Servlet进行处理。

(1)项目结构

假设项目结构如下:

MyWebApp/
│
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           ├── controller/
│   │   │           │   ├── LoginServlet.java
│   │   │           │   ├── StudyRoomServlet.java
│   │   │           │   └── SeatReservationServlet.java
│   │   │           └── model/
│   │   │               └── User.java
│   │   └── webapp/
│   │       ├── WEB-INF/
│   │       │   ├── web.xml
│   │       └── 
│   │           ├── login.jsp
│   │           ├── studyRoom.jsp
│   │           └── seatReservation.jsp
(2) User 模型类

首先,定义一个简单的User类来表示用户信息。

// src/main/java/com/example/model/User.java
package com.example.model;
 
public class User {
    private String username;
    private String password;
 
    // Getters and Setters
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
}
(3)登录页面 (login.jsp)

创建一个简单的登录页面。

<!-- src/main/webapp/login.jsp -->
<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h2>Login</h2>
    <form action="login" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" required><br><br>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password" required><br><br>
        <input type="submit" value="Login">
    </form>
    <p>Or go to <a href="studyRoom.jsp">Study Room Management</a> or <a href="seatReservation.jsp">Seat Reservation</a> (without login).</p>
</body>
</html>
(4)登录处理Servlet (LoginServlet.java)

处理登录表单提交的Servlet。

// src/main/java/com/example/controller/LoginServlet.java
package com.example.controller;
 
import com.example.model.User;
 
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 java.io.IOException;
 
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
 
        // Simple hard-coded authentication for demo purposes
        if ("admin".equals(username) && "password123".equals(password)) {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            request.getSession().setAttribute("user", user);
            response.sendRedirect("studyRoom.jsp");
        } else {
            response.sendRedirect("login.jsp?error=true");
        }
    }
}
(5)自习室管理页面 (studyRoom.jsp)

显示自习室管理页面(假设用户已登录)。

<!-- src/main/webapp/studyRoom.jsp -->
<!DOCTYPE html>
<html>
<head>
    <title>Study Room Management</title>
</head>
<body>
    <h2>Study Room Management</h2>
    <%
        if (session.getAttribute("user") == null) {
            response.sendRedirect("login.jsp");
            return;
        }
    %>
    <p>Welcome, <%= session.getAttribute("user").getUsername() %>!</p>
    <p>This is where you can manage study rooms.</p>
    <a href="logout">Logout</a>
</body>
</html>

JSP 复制 全屏

(6)座位预约页面 (seatReservation.jsp)

显示座位预约页面(假设用户未登录也可访问)。

<!-- src/main/webapp/seatReservation.jsp -->
<!DOCTYPE html>
<html>
<head>
    <title>Seat Reservation</title>
</head>
<body>
    <h2>Seat Reservation</h2>
    <p>You can reserve a seat here.</p>
    <%
        if (session.getAttribute("user") != null) {
            out.println("<p>Logged in as: " + session.getAttribute("user").getUsername() + "</p>");
        } else {
            out.println("<p>You are not logged in. <a href='login.jsp'>Login</a> to see more options.</p>");
        }
    %>
    <a href="login.jsp">Login</a>
</body>
</html>
(7)注销处理Servlet (LogoutServlet.java)

处理用户注销的Servlet(未包含在代码中,但可以通过添加一个新的Servlet实现)。


http://www.kler.cn/a/463517.html

相关文章:

  • 通义千问API KEY操作指南
  • 自动驾驶三维重建
  • RocketMQ面试题:进阶部分
  • 每天40分玩转Django:Django Celery
  • 基于51单片机和16X16LED点阵屏(74HC138和74HC595驱动)的小游戏《贪吃蛇》
  • 音频进阶学习九——离散时间傅里叶变换DTFT
  • C++ 基础概念: 未定义行为(Undefined Behavior)
  • 计算机创造的奇迹——C语言
  • GitHub Copilot免费上线!快速上手指南与功能解析
  • rouyi(前后端分离版本)配置
  • 【每日学点鸿蒙知识】动画主动停止、右滑左滑收拾、登录页跳转、Web组件拉起相册、怎么禁止侧滑
  • 快速增加ppt撤回次数的方法
  • 工厂模式与抽象工厂模式在Unity中的实际应用案例
  • mapper文件的解释
  • 【数据结构】数据结构简要介绍
  • C++并行处理支持库 之六
  • Oracle Dataguard(主库为 Oracle 11g 单节点)配置详解(3):配置备用数据库
  • 慧集通iPaaS集成平台低代码培训-基础篇
  • WebApi使用 (.Net Framework版)
  • 【AIGC】COT思维链:让AI学会拆解问题,像人一样思考
  • 【golang】go errors 处理错误追踪打印堆栈信息
  • idea 开发Gradle 项目
  • Linux:基础IO
  • HTML入门教程|| HTML 基本标签(2)
  • 第 28 章 - ES 源码篇 - Elasticsearch 启动与插件加载机制解析
  • 【每日学点鸿蒙知识】输入框光标显示问题、web组件回调async问题、图片加载流程监控、背景图片大小不生效问题、alert无效