学生管理系统

目录

基于servlet+jsp的学生管理系统

(一)代码目录结构

(二)最终页面效果

1.登录页面

 2.系统首页​编辑

3.功能页面(以查询页面为例)

​(三)系统功能介绍

(四)功能分析与实现

1.增删改查(以查功能为例)

2.十天内免登录功能


(一)代码目录结构

  • (二)最终页面效果

  • 1.登录页面

  •  2.系统首页

  • 3.功能页面(以查询页面为例)

  • (三)系统功能介绍

  1. 登录,注册功能
  2. 能对学生信息进行增删改查
  3. 利用监听器实现在线用户的统计
  4. 利用cookie实现十天内免登录功能
  5. 利用jstl和el表达式对jsp页面进行修饰

(四)功能分析与实现

1.增删改查(以查功能为例)

  • 实现这些功能,连接数据库是最基础的部分
  • 可以把连接数据库的信息,放到propreties文件,下次若连接别的数据库,或者说连接数据库信息变化时,不用修改java程序
  • 把零散的学生信息封装成学生对象,再把学生对象放到集合中
  • 把集合放在域对象里面
  • servlet做数据处理,jsp做页面展示
  //用容器list来装学生对象
        List<Stu> stus = new ArrayList<>();
        //连接数据库,获取学生信息
        Connection coon = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        ResourceBundle bundle  = ResourceBundle.getBundle("resources.jdbc");
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");
        //获取连接
        try {
            //注册驱动
            Class.forName(driver);
            //获取连接
             coon = DriverManager.getConnection(url, user, password);
            //执行SQL语句
            String sql = "select * from stu";
            ps = coon.prepareStatement(sql);
            rs = ps.executeQuery();
            //处理结果集
            while(rs.next()){
                //从结果集中取出来
                String stuId = rs.getString("stuId");
                String stuName = rs.getString("stuName");
                String stuSex = rs.getString("stuSex");
                String stuAge = rs.getString("stuAge");
                String stuWeight = rs.getString("stuWeight");
                //将以上零散的数据封装成一个java对象
                Stu stu  = new Stu();
                stu.setStuAge(stuAge);
                stu.setStuId(stuId);
                stu.setStuName(stuName);
                stu.setStuSex(stuSex);
                stu.setStuWeight(stuWeight);
                //将学生对象装进去
                stus.add(stu);
            }
            //释放资源
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }  finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (coon != null) {
                try {
                    coon.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        //将集合放到请求域中
        request.setAttribute("StuList",stus);

        //转发给下一个要展示的jsp页面
        // 转发(不要重定向)
        request.getRequestDispatcher("/lookStudent.jsp").forward(request, response);

  • jsp页面做数据展示
  • jstl,el表达式对jsp页面的Java代码优化
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/functions"  %>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>学生信息查询</title>
    </head>
    <body bgcolor="CCCFFF">
        <center>
            <br> <br> <br> <br> <br>
            你要查询的学生数据表中共有
            <font size="5" color="red">
                ${f:length(StuList)}
            </font>
            人
            <table border="2" bgcolor= "CCCEEE" width="600">
                <tr bgcolor="CCCCCC" align="center">
                    <th>记录条数</th>
                    <th>学号</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>年龄</th>
                    <th>体重</th>
                </tr>
            <c:forEach items="${StuList}" varStatus="stuStatus" var="stu">

                <tr align="center">
                    <td>${stuStatus.count}</td>
                    <td>${stu.stuId} </td>
                    <td>${stu.stuName} </td>
                    <td>${stu.stuSex} </td>
                    <td>${stu.stuAge}</td>
                    <td>${stu.stuWeight}</td>
                </tr>

            </c:forEach>

            </table>

        </center>
    </body>
</html>

2.十天内免登录功能

  • 登录页面勾选十天内免登录后,后端处理
  • 创建两个cookie,一个存储账号,一个存储密码,设置cookie有效时间,设置路径,响应给浏览器,登录信息会在电脑硬盘存储10天,下次访问,只要是cookie没有清除,不用 登录就可以访问
  • 可以做一个欢迎页面,访问系统默认访问,通过携带的cookie判段

    protected void doLogin(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        boolean success = false;
        // 你要做一件什么事儿?验证用户名和密码是否正确。
        // 获取用户名和密码
        // 前端你是这样提交的:username=admin&password=123
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 连接数据库验证用户名和密码
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from t_user where username = ? and password = ?";
            // 编译SQL
            ps = conn.prepareStatement(sql);
            // 给?传值
            ps.setString(1, username);
            ps.setString(2, password);
            // 执行SQL
            rs = ps.executeQuery();
            // 这个结果集当中最多只有一条数据。
            if (rs.next()) { // 不需要while循环
                // 登录成功
                success = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, rs);
        }

        // 登录成功/失败
        if (success) {
            // 获取session对象(这里的要求是:必须获取到session,没有session也要新建一个session对象。)
            HttpSession session = request.getSession(); // session对象一定不是null
            //session.setAttribute("username", username);

           User user = new User(username, password);
            session.setAttribute("user", user);

            // 登录成功了,并且用户确实选择了“十天内免登录”功能。
            String f = request.getParameter("f");
            if("1".equals(f)){
                // 创建Cookie对象存储登录名
                Cookie cookie1 = new Cookie("username", username);
                // 创建Cookie对象存储密码
                Cookie cookie2 = new Cookie("password", password); // 真实情况下是加密的。
                // 设置cookie的有效期为十天
                cookie1.setMaxAge(60 * 60 * 24 * 10);
                cookie2.setMaxAge(60 * 60 * 24 * 10);
                // 设置cookie的path(只要访问这个应用,浏览器就一定要携带这两个cookie)
                cookie1.setPath(request.getContextPath());
                cookie2.setPath(request.getContextPath());
                // 响应cookie给浏览器
                response.addCookie(cookie1);
                response.addCookie(cookie2);
            }

            // 成功,跳转到用户列表页面
            response.sendRedirect(request.getContextPath() + "/stuAdmin.jsp");
        } else {
            // 失败,跳转到失败页面
            response.sendRedirect(request.getContextPath() + "/error.jsp");
        }

    }
package com.student.web.action;
import com.student.bean.User;
import com.student.utils.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/welcome")
public class WelcomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取cookie
        // 这个Cookie[]数组可能是null,如果不是null,数组的长度一定是大于0的。
        Cookie[] cookies = request.getCookies();
        String username = null;
        String password = null;
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if("username".equals(name)){
                    username = cookie.getValue();
                }else if("password".equals(name)){
                    password = cookie.getValue();
                }
            }
        }

        // 要在这里使用username和password变量
        if(username != null && password != null){
            // 验证用户名和密码是否正确
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            boolean success = false;
            try {
                conn = DBUtil.getConnection();
                String sql = "select * from t_user where username = ? and password = ?";
                ps = conn.prepareStatement(sql);
                ps.setString(1,username);
                ps.setString(2,password);
                rs = ps.executeQuery();
                if (rs.next()) {
                    // 登录成功
                    success = true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(conn, ps, rs);
            }

            if (success) {
                // 获取session
                HttpSession session = request.getSession();
                //session.setAttribute("username", username);

                User user = new User(username, password);
                session.setAttribute("user", user);

                // 正确,表示登录成功
                response.sendRedirect(request.getContextPath() + "/stuAdmin.jsp");
            }else{
                // 错误,表示登录失败
                response.sendRedirect(request.getContextPath() + "/index.jsp");
            }
        }else{
            // 跳转到登录页面
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        }

    }
}

初学者,有问题请各位大神指正,若需要源码,请私信,谢谢大家

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SunBoyLisp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值