IDEA+Java+Servlet+JSP+Bootstrap+Mysql+Tomcat实现学生成绩管理系统(下)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: IDEA+Java+Servlet+JSP+Bootstrap+Mysql+Tomcat实现学生成绩管理系统

2.后端


Score

package vo;
/**
 * 分数类
 */
public class Score {
    private String id;
    private String database;
    private String android;
    private String jsp;
    public String getId(){
        return id;
    }
    public String getDatabase(){
        return database;
    }
    public String getAndroid(){
        return android;
    }
    public String getJsp(){
        return jsp;
    }
    public void setId(String id){
        this.id = id;
    }
    public void setDatabase(String database){
        this.database = database;
    }
    public void setAndroid(String android){
        this.android = android;
    }
    public void setJsp(String jsp){
        this.jsp = jsp;
    }
}

Student

package vo;
import java.sql.Date;
import java.sql.PseudoColumnUsage;
import java.util.regex.Pattern;
/**
 *学生类
 */
public class Student {
    private String id;
    private String password;
    private String name;
    private String sex;
    private String school_date;
    private String major;
    private String email;
    public String getId(){
        return id;
    }
    public String getPassword(){
        return password;
    }
    public String getName(){
        return name;
    }
    public String getSex(){
        return sex;
    }
    public String getSchool_date(){
        return school_date;
    }
    public String getMajor(){
        return major;
    }
    public String getEmail(){
        return email;
    }
    public void setId(String id){
        this.id = id;
    }
    public void setPassword(String password){
        this.password = password;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setSex(String sex){
        this.sex = sex;
    }
    public void setSchool_date(String school_date){
        this.school_date = school_date;
    }
    public void setMajor(String major){
        this.major = major;
    }
    public void setEmail(String email){
        this.email = email;
    }
}

Teacher

package vo;
/**
 * 教师类
 */
public class Teacher {
    private String id;
    private String password;
    private String email;
    private String name;
    private String sex;
    public String getId(){
        return id;
    }
    public String getPassword(){
        return password;
    }
    public String getEmail(){
        return email;
    }
    public String getName(){
        return name;
    }
    public String getSex(){
        return sex;
    }
    public void setId(String id){
        this.id = id;
    }
    public void setPassword(String password){
        this.password = password;
    }
    public void setEmail(String email){
        this.email= email;
    }
    public void setName(String name){
        this.name = name;
    }
    public void setSex(String sex){
        this.sex = sex;
    }
}

ScoreDao

package dao;
import vo.Score;
import java.sql.*;
import java.util.ArrayList;
public class ScoreDao {
    private Connection conn = null;
    // 定义数据常量
    static String driver = "com.mysql.cj.jdbc.Driver";
    static String url = "jdbc:mysql://localhost:3306/student_manager?serverTimezone=UTC";
    static String user = "root";
    static String password="admin";
    /**
     * 录入成绩
     * @param id
     * @return
     * @throws Exception
     */
    public boolean insertScore(String id) throws Exception{
        initConnection();
        String sql = "insert into score(id) values(?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, id);
        int i = ps.executeUpdate();
        closeConnection();
        return i == 1;
    }
    /**
     * 删除成绩
     * @param id
     * @return
     * @throws Exception
     */
    public boolean deleteScore(String id) throws Exception{
        initConnection();
        Statement stat = conn.createStatement();
        String sql = "delete from score where id='"+id+"'";
        int i = stat.executeUpdate(sql);
        closeConnection();
        return i==1;
    }
    /**
     * 更新成绩
     * @param id
     * @param database
     * @param android
     * @param jsp
     * @throws Exception
     */
    public void updateScoreInfo(String id, String database, String android, String jsp) throws Exception{
        initConnection();
        String sql = "update score set dat=?, android=?, jsp=? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, database);
        ps.setString(2, android);
        ps.setString(3, jsp);
        ps.setString(4, id);
        ps.executeUpdate();
        closeConnection();
    }
    /**
     * 以id查找信息
     * @param id
     * @return
     * @throws Exception
     */
    public Score findWithId(String id) throws Exception{
        initConnection();
        Statement stat = conn.createStatement();
        String sql = "select * from score where id = '" + id + "'";
        ResultSet rs = stat.executeQuery(sql);
        Score stu = getScore(rs);
        closeConnection();
        return stu;
    }
    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     * @throws Exception
     */
    public ArrayList<Score> getOnePage(int page, int size) throws Exception{
        ArrayList<Score> al = new ArrayList<>();
        initConnection();
        String sql = "SELECT * FROM score limit ?, ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, (page-1)*size);
        ps.setInt(2, size);
        ResultSet rs =  ps.executeQuery();
        getMoreScore(al, rs);
        closeConnection();
        return al;
    }
    /**
     * 查询总数
     * @return
     * @throws Exception
     */
    public int getScoreCount() throws Exception{
        initConnection();
        String sql = "select count(*) from score";
        Statement stat = conn.createStatement();
        ResultSet rs = stat.executeQuery(sql);
        rs.next();
        int count = rs.getInt(1);
        closeConnection();
        return count;
    }
    /**
     * 获取成绩
     * @param rs
     * @return
     * @throws SQLException
     */
    private Score getScore(ResultSet rs) throws SQLException {
        Score stu = null;
        if (rs.next()){
            stu = new Score();
            stu.setId(rs.getString("id"));
            stu.setDatabase(rs.getString("dat"));
            stu.setAndroid(rs.getString("android"));
            stu.setJsp(rs.getString("jsp"));
        }
        return stu;
    }
    /**
     * 获取所有成绩
     * @param al
     * @param rs
     * @throws SQLException
     */
    private void getMoreScore(ArrayList<Score> al, ResultSet rs) throws SQLException {
        while (rs.next()){
            Score score = new Score();
            score.setId(rs.getString("id"));
            score.setDatabase(rs.getString("dat"));
            score.setAndroid(rs.getString("android"));
            score.setJsp(rs.getString("jsp"));
            al.add(score);
        }
    }
    /**
     * 初始化数据库连接
     * @throws Exception
     */
    private void initConnection() throws Exception {
        Class.forName(driver);
        conn = DriverManager.getConnection(url, user, password);
    }
    /**
     * 关闭连接
     * @throws Exception
     */
    private void closeConnection() throws Exception{
        conn.close();
    }
}

StudentDao

package dao;
import vo.Student;
import java.sql.*;
import java.util.ArrayList;
/**
 * @author shuijianshiqing
 * @date 2020/6/11 20:40
 * 操作教师信息
 */
public class StudentDao {
    private Connection conn = null;
    // 定义数据常量
    static String driver = "com.mysql.cj.jdbc.Driver";
    static String url = "jdbc:mysql://localhost:3306/student_manager?serverTimezone=UTC";
    static String user = "root";
    static String password="admin";
    /**
     * 验证学生账号和密码
     * @param user
     * @param password
     * @return
     * @throws Exception
     */
    public Student checkAccount(String user, String password) throws Exception {
        initConnection();
        Statement stat = conn.createStatement();
        String sql = "select * from student where id = '" + user + "' and password = '" + password + "'";
        ResultSet rs = stat.executeQuery(sql);
        Student stu = getStudent(rs);
        closeConnection();
        return stu;
    }
    /**
     * 使用学生id来查找学生信息
     * @param id
     * @return
     * @throws Exception
     */
    public Student findWithId(String id) throws Exception{
        initConnection();
        Statement stat = conn.createStatement();
        String sql = "select * from student where id = '" + id + "'";
        ResultSet rs = stat.executeQuery(sql);
        Student stu = getStudent(rs);
        closeConnection();
        return stu;
    }
    /**
     * 使用学生姓名查找学生信息
     * @param name
     * @return
     * @throws Exception
     */
    public ArrayList<Student> findWithName(String name) throws Exception{
        ArrayList<Student> al = new ArrayList<>();
        initConnection();
        Statement stat = conn.createStatement();
        String sql = "select * from student where name = '" + name + "'";
        ResultSet rs = stat.executeQuery(sql);
        getMoreStudent(al, rs);
        closeConnection();
        return al;
    }
    /**
     * 添加学生信息
     * @param id
     * @param name
     * @param sex
     * @param school_date
     * @param major
     * @return
     * @throws Exception
     */
    public boolean insertStudent(String id, String name, String sex, String school_date, String major) throws Exception{
        initConnection();
        String sql = "insert into student(id, name, sex, school_date, major) values(?, ?, ?, ?, ?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, id);
        ps.setString(2, name);
        ps.setString(3, sex);
        ps.setString(4, school_date);
        ps.setString(5, major);
        int i = ps.executeUpdate();
        closeConnection();
        return i == 1;
    }
    /**
     * 删除学生信息
     * @param id
     * @return
     * @throws Exception
     */
    public boolean deleteStudent(String id) throws Exception{
        initConnection();
        Statement stat = conn.createStatement();
        String sql = "delete from student where id='"+id+"'";
        int i = stat.executeUpdate(sql);
        closeConnection();
        return i==1;
    }
    /**
     * 获取每页的信息
     * @param page
     * @param size
     * @return
     * @throws Exception
     */
    public ArrayList<Student> getOnePage(int page, int size) throws Exception{
        ArrayList<Student> al = new ArrayList<>();
        initConnection();
        String sql = "SELECT * FROM student limit ?, ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, (page-1)*size);
        ps.setInt(2, size);
        ResultSet rs =  ps.executeQuery();
        getMoreStudent(al, rs);
        closeConnection();
        return al;
    }
    /**
     * 获取学生总数
     * @return
     * @throws Exception
     */
    public int getStudentCount() throws Exception{
        initConnection();
        String sql = "select count(*) from student";
        Statement stat = conn.createStatement();
        ResultSet rs = stat.executeQuery(sql);
        rs.next();
        int count = rs.getInt(1);
        closeConnection();
        return count;
    }
    /**
     * 更新学生信息
     * @param id
     * @param name
     * @param sex
     * @param major
     * @throws Exception
     */
    public void updateStudentInfo(String id, String name, String sex, String major) throws Exception{
        initConnection();
        String sql = "update student set name=?, sex=?, major=? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, name);
        ps.setString(2, sex);
        ps.setString(3, major);
        ps.setString(4, id);
        ps.executeUpdate();
        closeConnection();
    }
    /**
     * 更新学生密码和邮件
     * @param id
     * @param email
     * @param password
     * @throws Exception
     */
    public void updateStudentSecurity(String id, String email, String password) throws Exception{
        initConnection();
        String sql = "update student set password=?, email=? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, password);
        ps.setString(2, email);
        ps.setString(3, id);
        ps.executeUpdate();
        closeConnection();
    }
    /**
     * 获取单个学生信息
     * @param rs
     * @return
     * @throws SQLException
     */
    private Student getStudent(ResultSet rs) throws SQLException {
        Student stu = null;
        if (rs.next()){
            stu = new Student();
            stu.setId(rs.getString("id"));
            stu.setPassword(rs.getString("password"));
            stu.setName(rs.getString("name"));
            stu.setSex(rs.getString("sex"));
            stu.setSchool_date(rs.getString("school_date"));
            stu.setMajor(rs.getString("major"));
            stu.setEmail(rs.getString("email"));
        }
        return stu;
    }
    /**
     * 获取多个学生信息
     * @param al
     * @param rs
     * @throws SQLException
     */
    private void getMoreStudent(ArrayList<Student> al, ResultSet rs) throws SQLException {
        while (rs.next()){
            Student stu = new Student();
            stu.setId(rs.getString("id"));
            stu.setPassword(rs.getString("password"));
            stu.setName(rs.getString("name"));
            stu.setSex(rs.getString("sex"));
            stu.setSchool_date(rs.getString("school_date"));
            stu.setMajor(rs.getString("major"));
            stu.setEmail(rs.getString("email"));
            al.add(stu);
        }
    }
    /**
     * 初始化数据库连接
     * @throws Exception
     */
    private void initConnection() throws Exception {
        Class.forName(driver);
        conn = DriverManager.getConnection(url, user, password);
    }
    /**
     * 关闭连接
     * @throws Exception
     */
    private void closeConnection() throws Exception{
        conn.close();
    }
}

TeacherDao

package dao;
import vo.Teacher;
import java.sql.*;
/**
 * @author shuijianshiqing
 * @date 2020/6/11 20:40
 * 操作教师信息
 */
public class TeacherDao {
    // 定义连接
    private Connection conn = null;
    // 定义数据常量
    static String driver = "com.mysql.cj.jdbc.Driver";
    static String url = "jdbc:mysql://localhost:3306/student_manager?serverTimezone=UTC";
    static String user = "root";
    static String password="admin";
    /**
     * 验证教师登录的账号和密码
     * @param id
     * @param password
     * @return
     * @throws Exception
     */
    public Teacher checkAccount(String id, String password) throws Exception {
        initConnection();
        Statement stat = conn.createStatement();
        String sql = "select * from teacher where id = '" + id + "' and password = '" + password + "'";
        ResultSet rs = stat.executeQuery(sql);
        Teacher tea = getTeacher(rs);
        closeConnection();
        return tea;
    }
    /**
     * 使用教师id来查询教师信息
     * @param id
     * @return
     * @throws Exception
     */
    public Teacher findWithId(String id) throws Exception {
        initConnection();
        Statement stat = conn.createStatement();
        String sql = "select * from teacher where id = '" + id + "'";
        ResultSet rs = stat.executeQuery(sql);
        Teacher tea = getTeacher(rs);
        closeConnection();
        return tea;
    }
    /**
     * 添加教师信息
     * @param id
     * @param password
     * @param email
     * @return
     * @throws Exception
     */
    public Teacher insertTeacher(String id, String password, String email) throws Exception {
        initConnection();
        String sql = "insert into teacher(id, password, email) values(?, ?, ?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, id);
        ps.setString(2, password);
        ps.setString(3, email);
        ps.executeUpdate();
        Teacher teacher = findWithId(id);
        closeConnection();
        return teacher;
    }
    /**
     * 更新教师信息
     * @param id
     * @param name
     * @param sex
     * @param email
     * @param password
     * @return
     * @throws Exception
     */
    public Teacher updateTeacher(String id, String name, String sex, String email, String password) throws Exception{
        initConnection();
        String sql = "update teacher set name=?, sex=?, email=?, password=? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, name);
        ps.setString(2, sex);
        ps.setString(3, email);
        ps.setString(4, password);
        ps.setString(5, id);
        ps.executeUpdate();
        Teacher teacher = findWithId(id);
        closeConnection();
        return teacher;
    }
    /**
     * 更新教师密码
     * @param id
     * @param password
     * @throws Exception
     */
    public void updateTeacherPassword(String id, String password) throws Exception{
        initConnection();
        String sql = "update teacher set password=? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, password);
        ps.setString(2, id);
        ps.executeUpdate();
        closeConnection();
    }
    /**
     * 根据ResultSet来获取教师信息
     * @param rs
     * @return
     * @throws SQLException
     */
    private Teacher getTeacher(ResultSet rs) throws SQLException {
        Teacher tea = null;
        if (rs.next()) {
            tea = new Teacher();
            tea.setId(rs.getString("id"));
            tea.setPassword(rs.getString("password"));
            tea.setName(rs.getString("name"));
            tea.setEmail(rs.getString("email"));
            tea.setSex(rs.getString("sex"));
        }
        return tea;
    }
    /**
     * 初始化数据库连接
     * @throws Exception
     */
    private void initConnection() throws Exception {
        Class.forName(driver);
        conn = DriverManager.getConnection(url, user, password);
    }
    /**
     * 关闭连接
     * @throws Exception
     */
    private void closeConnection() throws Exception {
        conn.close();
    }
    /**
     * 测试数据库连接
     * @param args
     */
    public static void main(String[] args) throws Exception{
        TeacherDao dao = new TeacherDao();
        dao.initConnection();
        System.out.println("数据库连接成功");
        dao.closeConnection();
    }
}

add_student

package servlet;
import dao.ScoreDao;
import dao.StudentDao;
import dao.StudentDao;
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.io.PrintWriter;
/**
 * 添加学生信息
 */
@WebServlet("/add_student")
public class add_student extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        StudentDao studentDao = new StudentDao();
        ScoreDao scoreDao = new ScoreDao();
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String major = request.getParameter("major");
        String school_date = request.getParameter("school_date");
        try {
            // 添加学生信息
            studentDao.insertStudent(id, name, sex, school_date, major);
            // 添加分数信息
            scoreDao.insertScore(id);
        }
        catch (Exception e){
            out.print(e);
        }
        response.sendRedirect("one_page_student");
    }
}

check_login

package servlet;
import dao.StudentDao;
import dao.TeacherDao;
import vo.Student;
import vo.Teacher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * 检查登录
 */
@WebServlet("/check_login")
public class check_login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
    protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        // 账号
        String user = request.getParameter("user");
        // 密码
        String password = request.getParameter("password");
        // 是否记住密码
        String remember = request.getParameter("remember");
        // 创建Dao层
        TeacherDao teacherDao = new TeacherDao();
        StudentDao studentDao = new StudentDao();
        // 创建实体类
        Teacher teacher = null;
        Student student = null;
        try {
            // 判断用户身份
            teacher = teacherDao.checkAccount(user, password);
            student = studentDao.checkAccount(user, password);
        }
        catch (Exception e) {
            out.print(e);
        }
        if (teacher != null) {
            //向session中添加用户信息
            session.setAttribute("info", teacher);
            //检查用户是否需要保持登录状态
            if (remember != null) {
                //发送cookie到客户端
                Cookie userCookie = new Cookie("name", user);
                userCookie.setMaxAge(10);
                response.addCookie(userCookie);
            }
            // 重定向
            response.sendRedirect("one_page_student");
        }
        else if (student != null){
            //向session中添加用户信息
            session.setAttribute("info", student);
            //检查用户是否需要保持登录状态
            if (remember != null) {
                //发送cookie到客户端
                Cookie userCookie = new Cookie("name", user);
                userCookie.setMaxAge(10);
                response.addCookie(userCookie);
            }
            response.sendRedirect("student/main.jsp");
        }
        else {
            out.print("<script>alert(\"用户名或密码错误!\");");
        }
    }
}

check_register

package servlet;
import dao.TeacherDao;
import vo.Teacher;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * 检查注册
 */
@WebServlet("/check_register")
public class check_register extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        String email = request.getParameter("email");
        String user = request.getParameter("user");
        String password = request.getParameter("password1");
        String code = request.getParameter("code");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        String randStr = (String) session.getAttribute("randStr");
        if (!code.equals(randStr)) {
            out.print("<script>alert(\"验证码错误!\");location.href = \"register.jsp\";</script>");
        } else {
            TeacherDao teacherDao = new TeacherDao();
            Teacher teacher = null;
            try {
                teacher = teacherDao.insertTeacher(user, password, email);
            } catch (Exception e) {
                out.print(e);
            }
            if (teacher != null) {
                //向session中添加用户信息
                session.setAttribute("info", teacher);
                response.sendRedirect("one_page_student");
            } else {
                out.print("<script>alert(\"此用户已经注册!\");location.href = \"register.jsp\";</script>");
            }
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}

delete_student

package servlet;
import dao.ScoreDao;
import dao.StudentDao;
import dao.StudentDao;
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.io.PrintWriter;
/**
 * 删除学生信息
 */
@WebServlet("/delete_student")
public class delete_student extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        StudentDao studentDao = new StudentDao();
        ScoreDao scoreDao = new ScoreDao();
        String id = request.getParameter("id");
        try {
            // 删除学生
            studentDao.deleteStudent(id);
            // 删除成绩
            scoreDao.deleteScore(id);
            response.sendRedirect("one_page_student");
        }
        catch (Exception e){
            out.print(e);
        }
    }
}

exit

package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * 退出登录
 */
@WebServlet("/exit")
public class exit extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //清除cookie, 跳到起始页
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie c : cookies) {
                String cookieName = c.getName();
                if ("name".equals(cookieName)) {
                    c.setMaxAge(0);
                    response.addCookie(c);
                }
            }
        }
        response.sendRedirect("index.jsp");
    }
}

one_page_score

package servlet;
import dao.ScoreDao;
import vo.Score;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
/**
 * 分数分页
 */
@WebServlet("/one_page_score")
public class one_page_score extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        // 获取传入id的值
        String key = request.getParameter("id");
        if (key == null) {
            int currentIndex, count, size = 10;
            String index = request.getParameter("index");
            if (index == null)
                index = "1";
            currentIndex = Integer.parseInt(index);
            try {
                ScoreDao scoD = new ScoreDao();
                count = scoD.getScoreCount();
                ArrayList<Score> stus = scoD.getOnePage(currentIndex, size);
                int sumIndex = count % size == 0 ? count / size : count / size + 1;
                session.setAttribute("onePageScore", stus);
                session.setAttribute("sumScoreIndex", sumIndex);
                response.sendRedirect("teacher/score.jsp");
            } catch (Exception e) {
                out.print(e);
            }
        }
        else {
            ScoreDao scoreDao = new ScoreDao();
                try {
                    Score score = scoreDao.findWithId(key);
                    ArrayList<Score> scores = new ArrayList<>();
                    scores.add(score);
                    session.setAttribute("onePageScore", scores);
                    session.setAttribute("sumScoreIndex", 1);
                    response.sendRedirect("teacher/score.jsp");
                } catch (Exception e) {
                    out.print(e);
                }
            }
        }
}

one_page_student

package servlet;
import dao.StudentDao;
import vo.Student;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.regex.Pattern;
/**
 *学生分页
 */
@WebServlet("/one_page_student")
public class one_page_student extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        // 获取key的值
        String key = request.getParameter("key");
        // 如果key为空或者空字符串
        if (key == null || key.equals("")) {
            int currentIndex, count, size = 10;
            String index = request.getParameter("index");
            if (index == null)
                index = "1";
            // 当前页赋值
            currentIndex = Integer.parseInt(index);
            try {
                StudentDao sdao = new StudentDao();
                // 查询当前页信息
                ArrayList<Student> stus = sdao.getOnePage(currentIndex, size);
                count = sdao.getStudentCount();
                // 计算总页数
                int sumIndex = count % size == 0 ? count / size : count / size + 1;
                session.setAttribute("onePageStudent", stus);
                session.setAttribute("sumIndex", sumIndex);
                response.sendRedirect("teacher/main.jsp");
            } catch (Exception e) {
                out.print(e);
            }
        }
        else {
            StudentDao studentDao = new StudentDao();
            // 这里前一个"\"是转义字符,后一个是字符串,这个字符串表示数字至少出现一次
            String pattern = "^\\d+";
            boolean isMatch = Pattern.matches(pattern, key);
            if (isMatch) {
                try {
                    // 某个学生的信息
                    Student student = studentDao.findWithId(key);
                    ArrayList<Student> students = new ArrayList<>();
                    students.add(student);
                    session.setAttribute("onePageStudent", students);
                    session.setAttribute("sumIndex", 1);
                    response.sendRedirect("teacher/main.jsp");
                } catch (Exception e) {
                    out.print(e);
                }
            } else {
                try {
                    ArrayList<Student> stus = studentDao.findWithName(key);
                    session.setAttribute("onePageStudent", stus);
                    session.setAttribute("sumIndex", 1);
                    response.sendRedirect("teacher/main.jsp");
                } catch (Exception e) {
                    out.print(e);
                }
            }
        }
    }
}

update_score

package servlet;
import dao.ScoreDao;
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.io.PrintWriter;
/**
 * 更新成绩
 */
@WebServlet("/update_score")
public class update_score extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        ScoreDao scoreDao = new ScoreDao();
        // 把要修改的值存下来
        String[] id = request.getParameterValues("id");
        String[] database = request.getParameterValues("database");
        String[] android = request.getParameterValues("android");
        String[] jsp = request.getParameterValues("jsp");
        try {
            // 循环去更新
            for (int i=0; i<id.length; i++) {
                scoreDao.updateScoreInfo(id[i], database[i], android[i], jsp[i]);
            }
            response.sendRedirect("one_page_score");
        }
        catch (Exception e){
            out.print(e);
        }
    }
}

update_student

package servlet;
import dao.StudentDao;
import dao.StudentDao;
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.io.PrintWriter;
/**
 * 更新学生信息
 */
@WebServlet("/update_student")
public class update_student extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        StudentDao studentDao = new StudentDao();
        String stuno = request.getParameter("stuno");
        String stuname = request.getParameter("stuname");
        String stusex = request.getParameter("stusex");
        String stumajor = request.getParameter("stumajor");
        try {
            // 更新学生信息
            studentDao.updateStudentInfo(stuno, stuname, stusex, stumajor);
        }
        catch (Exception e){
            out.print(e);
        }
        response.sendRedirect("one_page_student");
    }
}

update_teacher

package servlet;
import dao.TeacherDao;
import vo.Teacher;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * 更新教师信息
 */
@WebServlet("/update_teacher")
public class update_teacher extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        TeacherDao teacherDao = new TeacherDao();
        String uid = request.getParameter("uid");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        try {
            // 更新教师信息
            Teacher teacher = teacherDao.updateTeacher(uid, name, sex, email, password);
            session.setAttribute("info", teacher);
            out.print("<script>alert(\"保存成功!\");location.href = \"teacher/personal.jsp\";</script>");
        }
        catch (Exception e){
            out.print(e);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}

upload_studentImg

package servlet;
import com.jspsmart.upload.File;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import javax.servlet.ServletConfig;
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.io.PrintWriter;
/**
 * 更新学生头像
 */
@WebServlet("/upload_studentImg")
public class upload_studentImg extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        SmartUpload smartUpload = new SmartUpload();
        Request rq = smartUpload.getRequest();
        ServletConfig config = this.getServletConfig();
        smartUpload.initialize(config, request, response);
        try {
            //上传文件
            smartUpload.upload();
            String id = rq.getParameter("id");
            File smartFile = smartUpload.getFiles().getFile(0);
            smartFile.saveAs("/userImg/"+id+".jpeg");
            out.print("<script>alert(\"上传成功!\");window.location.href='student/personal.jsp';</script>");
        }
        catch (Exception e){
            out.print(e);
        }
    }
}

upload_teacherImg

package servlet;
import com.jspsmart.upload.File;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import javax.servlet.ServletConfig;
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.io.PrintWriter;
/**
 * 更新教师头像
 */
@WebServlet("/upload_teacherImg")
public class upload_teacherImg extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        SmartUpload smartUpload = new SmartUpload();
        Request rq = smartUpload.getRequest();
        ServletConfig config = this.getServletConfig();
        smartUpload.initialize(config, request, response);
        try {
            //上传文件
            smartUpload.upload();
            String id = rq.getParameter("id");
            File smartFile = smartUpload.getFiles().getFile(0);
            smartFile.saveAs("/userImg/"+id+".jpeg");
            out.print("<script>alert(\"上传成功!\");window.location.href='teacher/personal.jsp';</script>");
        }
        catch (Exception e){
            out.print(e);
        }
    }
}


所用到的jar包


itextpdf-5.5.5.jar

javax.annotation.jar

javax.ejb.jar

javax.jms.jar

javax.persistence.jar

javax.resource.jar

javax.servlet.jar

javax.servlet.jsp.jar

javax.servlet.jsp.jstl.jar

javax.transaction.jar

jsmartcom_zh_CN.jar

mysql-connector-java-8.0.15.jar


四、感悟及商务合作


这个系统用到了Servlet,相较于上个项目,很多页面的逻辑可以转到后台进行处理,前端的jsp页面省了好多事情。这样前端页面加载也快一点。


1.项目来源

这个项目是从学妹哪里拿过来的,自己做了一些代码的注释和修改工作,这个项目基本的JavaEE的知识点都包括了。但是没有用到日志框架,不过用来做简单的课程设计是绰绰有余了。


2.图书管理系统

要是需要图书管理系统的可以访问博客,请移步:

图书管理系统

图书管理系统-添加管理增删改查功能

Java+JSP+Mysql+Tomcat实现图书管理系统


3.IDEA创建Web项目

要是不知道IDEA如何搭建WEB项目,请移步:

IDEA创建Web项目详细说明


五、其他


1.其他系统实现


JavaWeb系统系列实现

Java+JSP实现学生图书管理系统

Java+JSP实现学生信息管理系统

Java+Servlet+JSP实现学生成绩管理系统

Java+Servlet+JSP实现宠物诊所管理系统

Java+SSM+Easyui实现网上考试系统

Java+Springboot+H-ui实现营销管理系统

Java+Springboot+Bootstrap实现网上商城系统


JavaSwing系统系列实现

Java+Swing实现斗地主游戏

Java+Swing实现图书管理系统

Java+Swing实现医院管理系统

Java+Swing实现仓库管理系统

Java+Swing实现考试管理系统

Java+Swing实现通讯录管理系统

Java+Swing实现停车场管理系统

Java+Swing实现学生信息管理系统

Java+Swing实现学生宿舍管理系统

Java+Swing实现学生选课管理系统

Java+Swing实现学生成绩管理系统

Java+Swing实现学校教材管理系统

Java+Swing实现学校教务管理系统

Java+Swing实现企业人事管理系统

Java+Swing实现电子相册管理系统

Java+Swing实现自助取款机(ATM)系统

Java+Swing实现超市管理系统-TXT存储信息

Java+Swing实现宠物商店管理系统-TXT存储信息


2.获取源码


点击以下链接获取源码

Java+Servlet+JSP+Bootstrap+Mysql学生成绩管理系统源码


3.备注


如有侵权请联系我删除。


4.鸡汤


每日反思,每日进步。


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
15天前
|
Java 关系型数据库 MySQL
Java语言与MySQL数据库连接的技术性探讨
Java语言与MySQL数据库连接的技术性探讨
|
22天前
|
SQL Java 关系型数据库
JavaWeb(JDBC编程)看这一篇就够了 —— 如何使用Java操作mysql数据库
JavaWeb(JDBC编程)看这一篇就够了 —— 如何使用Java操作mysql数据库
16 0
|
26天前
|
监控 NoSQL Java
java云MES 系统源码Java+ springboot+ mysql 一款基于云计算技术的企业级生产管理系统
MES系统是生产企业对制造执行系统实施的重点在智能制造执行管理领域,而MES系统特点中的可伸缩、信息精确、开放、承接、安全等也传递出:MES在此管理领域中无可替代的“王者之尊”。MES制造执行系统特点集可伸缩性、精确性、开放性、承接性、经济性与安全性于一体,帮助企业解决生产中遇到的实际问题,降低运营成本,快速适应企业不断的制造执行管理需求,使得企业已有基础设施与一切可用资源实现高度集成,提升企业投资的有效性。
65 5
|
应用服务中间件 Windows
拆解Tomcat10: (二) 在Idea中调试最新的Tomcat10源码(三)
拆解Tomcat10: (二) 在Idea中调试最新的Tomcat10源码
152 0
拆解Tomcat10: (二) 在Idea中调试最新的Tomcat10源码(三)
|
Java 应用服务中间件 Apache
拆解Tomcat10: (二) 在Idea中调试最新的Tomcat10源码(二)
拆解Tomcat10: (二) 在Idea中调试最新的Tomcat10源码
391 0
拆解Tomcat10: (二) 在Idea中调试最新的Tomcat10源码(二)
|
IDE Java 应用服务中间件
拆解Tomcat10: (二) 在Idea中调试最新的Tomcat10源码(一)
拆解Tomcat10: (二) 在Idea中调试最新的Tomcat10源码
266 0
拆解Tomcat10: (二) 在Idea中调试最新的Tomcat10源码(一)
|
IDE Java 应用服务中间件
拆解Tomcat10: (二) 在Idea中调试最新的Tomcat10源码
可以到Tomcat官网下载源码压缩包,也可以去GitHub仓库Clone。 目前最新的是2021-12-08发布的10.0.14版。
514 0
拆解Tomcat10: (二) 在Idea中调试最新的Tomcat10源码
|
Java 应用服务中间件 容器
Idea 调试Tomcat源码
开篇  不知道从哪个途径被安利买了一本《Tomcat架构解析》的书,突然对Tomcat来了兴趣,准备花一段时间好好研究这个对自己来说是黑盒的容器,所以在未来一段时间里朋友圈应该会有大量这方面的分享。
3787 0
|
2天前
|
Java 编译器 Maven
使用intellij idea搭建SSM架构的maven项目 详细
使用intellij idea搭建SSM架构的maven项目 详细
17 4
|
2天前
|
Java Maven
使用intellij idea搭建SSM架构的maven项目 配置文件
使用intellij idea搭建SSM架构的maven项目 配置文件
12 0