JavaWeb实现学生管理系统

一、项目介绍

该系统主要用到的技术主要有Toncat、Servlet、JSP、JavaBean、Filters、JDBC、MySQL。通过这些技术来实现学生注册登录、选课、对课程进行增删改查以及管理员注册登录并对学生所选课程进行查询修改等,主要文件如下图所示。

JavaWeb实现学生管理系统_第1张图片

 JavaWeb实现学生管理系统_第2张图片

二、注册登录

注册登录主要逻辑是:在登陆时先判断一下该账号是否注册,如果注册过就直接验证密码,复合就登陆成功,如果未注册就进行相关的注册操作,注册成功后将其存入相关的数据库,登录时再次进行验证。

用户注册代码:

JavaWeb实现学生管理系统_第3张图片

package servlet;

import utils.JDBCUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.*;

@WebServlet(name = "UserRegisterServlettest", urlPatterns = "/register")
public class UserRegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("注册请求过来了");

        ResultSet rs = null;  //结果集
        Connection connection = null;  //连接数据库
        PreparedStatement ps = null;  //预编译
        Statement statement = null;  //数据库对象

        //        连接到数据库
        try {
            connection = JDBCUtils.getConnection();
            statement = connection.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }

        String account = request.getParameter("account");
        String password = request.getParameter("password");
        System.out.println("这是输入框的账号和密码" + ":" + account + "   " + password);

        String sql = "select * from studentmessage where account = ? and password = ?";
        String sqlthree = "select * from studentmessage where account = ? and password = ? and isdelete = 1";



        try {
            ps = connection.prepareStatement(sqlthree);
            ps.setObject(1, account);
            ps.setObject(2, password);
            rs = ps.executeQuery();  //返回true或者false  查到了是true

            System.out.println(rs);

            if (rs.next()){
//                修改iddelete的状态
                System.out.println("这个是修改isdelete状态的代码");
                String sqlfour = "update studentmessage set isdelete = 0 where account = ?";
                ps = connection.prepareStatement(sqlfour);
                ps.setObject(1, account);
                ps.executeUpdate();
                request.getRequestDispatcher("user_login.jsp").forward(request, response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }




        try {
            ps = connection.prepareStatement(sql);
            ps.setObject(1, account);
            ps.setObject(2, password);
            rs = ps.executeQuery();  //返回true或者false  查到了是true

            System.out.println(rs);

            if (!rs.next()) {
//                数据库没有该账号,增加
                String insertsql = "insert into studentmessage VALUE(?,?,?,?)";
                System.out.println(insertsql);
                ps = connection.prepareStatement(insertsql);
                ps.setObject(1, 0);
                ps.setObject(2, account);
                ps.setObject(3, password);
                ps.setObject(4, 0);
                ps.executeUpdate();
                request.getRequestDispatcher("user_login.jsp").forward(request, response);
            }

            else {
//                用户已经注册,无法再次注册
                System.out.println("该用户已注册,请重新注册");
                request.setAttribute("msg", "该用户已注册,请重新注册");
                request.getRequestDispatcher("user_register.jsp").forward(request, response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

用户登录代码:

JavaWeb实现学生管理系统_第4张图片

package servlet;

import dao.UserDao;
import model.Student;
import service.UserService;
import utils.JDBCUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "UserRegisterServlet", urlPatterns = "/center")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("登录请求过来了");

        Connection conn = null;  //连接到数据库
        PreparedStatement pstm = null;  //预编译
        ResultSet rs = null;  //结果集
        Statement statement = null;

//        连接到数据库
        try {
            conn = JDBCUtils.getConnection();
            statement = conn.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //输入框的账号和密码
        String act = request.getParameter("account");
        String pswd = request.getParameter("password");
        System.out.println("这是输入框的账号密码:" + act + "   " + pswd);
        String sql = "select * from studentmessage where account = ? and password = ? and isdelete = 0";
        String a = "15237372817";

//      将账号存入Session
        HttpSession zh = request.getSession();
        zh.setAttribute("account",act);
        System.out.println("这个是session"+":" + zh.getAttribute("account"));



//      验证
        try {
            pstm = conn.prepareStatement(sql);
            pstm.setObject(1, act);
            pstm.setString(2, pswd);
            rs = pstm.executeQuery();

            if (rs.next() == false) {
                System.out.println(123);
                request.setAttribute("failMsg", "你输入的信息有误,请重新登录");
                request.getRequestDispatcher("user_login.jsp").forward(request, response);
            } else {
                String account = rs.getString(2);
                String password = rs.getString(3);
                System.out.println("这是数据库的账号密码:" + account + " " + password);

                //                管理员登录
                String aa = "15237372817";
                if (act.equals(aa) && pswd.equals(aa)) {
                    System.out.println("这个是管理员登录");
                    request.getRequestDispatcher("administrators.jsp").forward(request, response);
                }else {
                    System.out.println("欢迎来到个人中心");
                    request.setAttribute("msg", "欢迎来到个人中心");
                    request.getRequestDispatcher("user_center.jsp").forward(request, response);
                }

//                if (account.equals(act) && password.equals(pswd)) {
//                    System.out.println("欢迎来到个人中心");
//                    request.setAttribute("msg", "欢迎来到个人中心");
//                    request.getRequestDispatcher("user_center.jsp").forward(request, response);
//                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

三、学生服务

进入个人中心之后,会有更换密码、注销账户、选择科目三个功能

  1. 更换密码
    用户进行自己账号的输入,然后输入新秘密,之后提交,新密码即设置成功JavaWeb实现学生管理系统_第5张图片
    package servlet;
    
    import utils.JDBCUtils;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.annotation.*;
    import java.io.IOException;
    import java.sql.*;
    
    @WebServlet(name = "UserChangePwdServlet", urlPatterns = "/UserChangePwd")
    public class UserChangePwdServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            System.out.println("更改请求过来了");
    
            Connection conn = null;
            Statement statement = null;
            PreparedStatement preparedStatement = null;
            boolean resultSet = true;
    
            //        连接到数据库
            try {
                conn = JDBCUtils.getConnection();
                statement = conn.createStatement();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            String account = request.getParameter("account");
            String newPassword = request.getParameter("newPassword");
    
            String sql = "update studentmessage set password = ? where account = ?";
    
            try {
                assert conn != null;
                preparedStatement = conn.prepareStatement(sql);
                preparedStatement.setObject(1, newPassword);
                preparedStatement.setObject(2, account);
                resultSet = preparedStatement.execute();
                request.getRequestDispatcher("user_login.jsp").forward(request, response);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
  2. 注销账户
    输入账号,进行账号的注销JavaWeb实现学生管理系统_第6张图片
    package servlet;
    
    import utils.JDBCUtils;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.annotation.*;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    @WebServlet(name = "UserLogoutServlet", urlPatterns = "/logout")
    public class UserLogoutServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            System.out.println("注销请求过来了");
    
            PreparedStatement preparedStatement = null;
            Connection connection = null;
            Statement statement = null;
    
            //        连接到数据库
            try {
                connection = JDBCUtils.getConnection();
                statement = connection.createStatement();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            String account = request.getParameter("outaccount");
            String sql = "update studentmessage set isdelete = 1 where account = ?";
            System.out.println(account);
    
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setObject(1,account);
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            request.getRequestDispatcher("user_login.jsp").forward(request, response);
        }
    }
  3. 选择科目
    用户可以选择自己想选择的课程提交,提交后会在数据库保存
    JavaWeb实现学生管理系统_第7张图片

package servlet;

import utils.JDBCUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.*;

@WebServlet(name = "UserCourseServlet", urlPatterns = "/course")
public class UserCourseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("选课请求过来了");

        ResultSet rs = null;  //结果集
        PreparedStatement ps = null;  //预编译
        Statement statement = null;  //数据库对象
        Connection connection = null;


        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        try {
            connection = JDBCUtils.getConnection();
            statement = connection.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }

        String account = request.getParameter("account");

        String chinese = request.getParameter("chinese");
        String math = request.getParameter("math");
        String eng = request.getParameter("eng");
        String phy = request.getParameter("phy");
        String che = request.getParameter("che");
        String bio = request.getParameter("bio");

        String course = "";

        if (chinese != null){
            course = course + chinese + " ";
        }
        if (math != null){
            course = course + math + " ";
        }
        if (eng != null){
            course = course + eng + " ";
        }
        if (phy != null){
            course = course + phy + " ";
        }
        if (che != null){
            course = course + che + " ";
        }
        if (bio != null){
            course = course + bio;
        }

        System.out.println(course);

        String sql = "insert into studentchoose VALUE (?,?,?)";
        String twosql = "select * from studentchoose where account = ?";
        String threesql = "update studentchoose set course = ? where account = ?";


        try {
            assert false;
            System.out.println("这个是执行内容");
            ps = connection.prepareStatement(sql);
            ps.setObject(1, 0);
            ps.setObject(2,account);
            ps.setObject(3,course);
            ps.executeUpdate();
            request.getRequestDispatcher("course_success.jsp").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

 四、管理员服务

管理员有特定的账号,可以对学生所选的课程进行查询、增加、修改和删除

JavaWeb实现学生管理系统_第8张图片

 

  1. 增加课程
    点击增加,可进入增加页面,页面会显示当前该生的选课情况,之后填写想增加的内容提交即可。
    JavaWeb实现学生管理系统_第9张图片

    package servlet;
    
    import model.administrators;
    import utils.JDBCUtils;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.annotation.*;
    import java.io.IOException;
    import java.sql.*;
    import java.util.ArrayList;
    
    @WebServlet(name = "plusServlet",urlPatterns = "/plus")
    public class plusServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            System.out.println("增加请求过来了");
    
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
    
            HttpSession  session = request.getSession();
    //        这个是Session里边的集合
            ArrayList pluslist = (ArrayList) session.getAttribute("administrators");
            System.out.println(pluslist);
    //      更改框的内容
            String plus = request.getParameter("plus");
            System.out.println(plus);
    //      获取第几条数据以及该数据的账号
            String account = pluslist.get(0).getAccount();
            String course = pluslist.get(0).getCourse();
            System.out.println(account);
    
    
    
    
    
            Connection conn = null;  //连接到数据库
            PreparedStatement ps = null;  //预编译
            Statement statement = null;
    
            //        连接到数据库
            try {
                conn = JDBCUtils.getConnection();
                statement = conn.createStatement();
            } catch (Exception e) {
                e.printStackTrace();
            }
    //"+ course + "
            String sql = "update studentchoose set course = ? where account = ? ";
    
            try {
                assert conn != null;
                ps = conn.prepareStatement(sql);
                ps.setObject(1,course+plus);
                ps.setObject(2,account);
                ps.executeUpdate();
    
                request.getRequestDispatcher("plussuccess.jsp").forward(request, response);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    

  2. 修改课程
    填写想要修改为什么科目,提交后即可
    JavaWeb实现学生管理系统_第10张图片

    package servlet;
    
    import model.administrators;
    import utils.JDBCUtils;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.annotation.*;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    
    @WebServlet(name = "changecourseServlet", urlPatterns = "/changecourse")
    public class changecourseServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("改变课程请求过来了");
    
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
    
            HttpSession session = request.getSession();
    //        这个是Session里边的集合
            ArrayList pluslist = (ArrayList) session.getAttribute("administrators");
            System.out.println(pluslist);
    //      更改框的内容
            String plus = request.getParameter("plus");
            System.out.println(plus);
    //      获取第几条数据以及该数据的账号
            String account = pluslist.get(0).getAccount();
            String course = pluslist.get(0).getCourse();
            System.out.println(account);
    
            String changecourse = request.getParameter("changecourse");
    
            Connection conn = null;  //连接到数据库
            PreparedStatement ps = null;  //预编译
            Statement statement = null;
    
            //        连接到数据库
            try {
                conn = JDBCUtils.getConnection();
                statement = conn.createStatement();
            } catch (Exception e) {
                e.printStackTrace();
            }
    //"+ course + "
            String sql = "update studentchoose set course = ? where account = ? ";
    
            try {
                assert conn != null;
                ps = conn.prepareStatement(sql);
                ps.setObject(1, changecourse);
                ps.setObject(2, account);
                ps.executeUpdate();
    
                request.getRequestDispatcher("changesuccess.jsp").forward(request, response);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
    
        }
    }
    
  3. 删除课程
    填写想要删除的课程,提交即可
    JavaWeb实现学生管理系统_第11张图片

    package servlet;
    
    import model.administrators;
    import utils.JDBCUtils;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.annotation.*;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    
    @WebServlet(name = "removeServlet", urlPatterns = "/remove")
    public class removeServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("删除请求过来了");
    
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
    
            HttpSession session = request.getSession();
    //      这个是Session里边的集合
            ArrayList pluslist = (ArrayList) session.getAttribute("administrators");
            System.out.println(pluslist);
    //      删除框的内容
            String remove = request.getParameter("remove");
            System.out.println(remove);
    //      获取第几条数据以及该数据的账号
            String account = pluslist.get(0).getAccount();
            String course = pluslist.get(0).getCourse();
            System.out.println(account);
    //
    
            Connection conn = null;  //连接到数据库
            PreparedStatement ps = null;  //预编译
            Statement statement = null;
    
            //        连接到数据库
            try {
                conn = JDBCUtils.getConnection();
                statement = conn.createStatement();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            String sql = "update studentchoose set course = ? where account = ? ";
    
            try {
                assert conn != null;
                ps = conn.prepareStatement(sql);
                ps.setObject(1, course);
                ps.setObject(2, account);
                ps.executeUpdate();
    
                request.getRequestDispatcher("removesuccess.jsp").forward(request, response);
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
    
        }
    }
    

你可能感兴趣的:(java,web,mysql)