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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 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.鸡汤


每日反思,每日进步。


相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3天前
|
Java 数据库连接 数据库
深入探讨Java连接池技术如何通过复用数据库连接、减少连接建立和断开的开销,从而显著提升系统性能
在Java应用开发中,数据库操作常成为性能瓶颈。本文通过问题解答形式,深入探讨Java连接池技术如何通过复用数据库连接、减少连接建立和断开的开销,从而显著提升系统性能。文章介绍了连接池的优势、选择和使用方法,以及优化配置的技巧。
8 1
|
5天前
|
JavaScript Java 项目管理
Java毕设学习 基于SpringBoot + Vue 的医院管理系统 持续给大家寻找Java毕设学习项目(附源码)
基于SpringBoot + Vue的医院管理系统,涵盖医院、患者、挂号、药物、检查、病床、排班管理和数据分析等功能。开发工具为IDEA和HBuilder X,环境需配置jdk8、Node.js14、MySQL8。文末提供源码下载链接。
|
8天前
|
移动开发 前端开发 JavaScript
java家政系统成品源码的关键特点和技术应用
家政系统成品源码是已开发完成的家政服务管理软件,支持用户注册、登录、管理个人资料,家政人员信息管理,服务项目分类,订单与预约管理,支付集成,评价与反馈,地图定位等功能。适用于各种规模的家政服务公司,采用uniapp、SpringBoot、MySQL等技术栈,确保高效管理和优质用户体验。
|
27天前
|
Java 容器
【学习笔记】Jsp与Servlet技术
【学习笔记】Jsp与Servlet技术
60 0
|
3月前
|
SQL Java 数据库
jsp中使用Servlet查询SQLSERVER数据库中的表的信息,并且打印在屏幕上
该博客文章介绍了在JSP应用中使用Servlet查询SQL Server数据库的表信息,并通过JavaBean封装图书信息,将查询结果展示在Web页面上的方法。
jsp中使用Servlet查询SQLSERVER数据库中的表的信息,并且打印在屏幕上
|
3月前
|
供应链 前端开发 Java
JSP+servlet+mybatis+layui服装库存管理系统(大三上学期课程设计)
这篇文章通过一个服装库存管理系统的实例,展示了在Spring Boot项目中使用Ajax、JSON、layui、MVC架构和iframe等技术,涵盖了注册登录、权限管理、用户管理、库存管理等功能,并提供了系统运行环境和技术要求的详细说明。
JSP+servlet+mybatis+layui服装库存管理系统(大三上学期课程设计)
|
3月前
|
前端开发 安全 Java
在Java服务器端开发的浩瀚宇宙中,Servlet与JSP犹如两颗璀璨的明星,它们联袂登场,共同编织出动态网站的绚丽篇章。
在Java服务器端开发的浩瀚宇宙中,Servlet与JSP犹如两颗璀璨的明星,它们联袂登场,共同编织出动态网站的绚丽篇章。
27 0
|
5月前
|
自然语言处理 前端开发 Java
Servlet与JSP:Java Web开发的基石技术详解
【6月更文挑战第23天】Java Web的Servlet与JSP是动态网页的核心。Servlet是服务器端的Java应用,处理HTTP请求并响应;JSP则是结合HTML与Java代码的页面,用于动态内容生成。Servlet通过生命周期方法如`init()`、`service()`和`destroy()`工作,而JSP在执行时编译成Servlet。两者在MVC架构中分工,Servlet处理逻辑,JSP展示数据。尽管有Spring MVC等框架,Servlet和JSP仍是理解Web开发基础的关键。
100 12
|
5月前
|
存储 Java 关系型数据库
基于Servlet和JSP的Java Web应用开发指南
【6月更文挑战第23天】构建Java Web应用,Servlet与JSP携手打造在线图书管理系统,涵盖需求分析、设计、编码到测试。通过实例展示了Servlet如何处理用户登录(如`LoginServlet`),JSP负责页面展示(如`login.jsp`和`bookList.jsp`)。应用基于MySQL数据库,包含用户和图书表。登录失败显示错误信息,成功后展示图书列表。部署到Tomcat服务器测试功能。此基础教程为深入Java Web开发奠定了基础。
98 10
|
5月前
|
缓存 小程序 前端开发
Java服务器端技术探秘:Servlet与JSP的核心原理
【6月更文挑战第23天】Java Web开发中的Servlet和JSP详解:Servlet是服务器端的Java小程序,处理HTTP请求并响应。生命周期含初始化、服务和销毁。创建Servlet示例代码展示了`doGet()`方法的覆盖。JSP则侧重视图,动态HTML生成,通过JSP脚本元素、声明和表达式嵌入Java代码。Servlet常作为控制器,JSP处理视图,遵循MVC模式。优化策略涉及缓存、分页和安全措施。这些技术是Java服务器端开发的基础。
58 9

推荐镜像

更多