利用JSP技术采用MVC三层模式建立一个简单的学生管理系统(基于chatgpt)

目录

模型层(Student.java):

数据访问对象层(StudentDAO.java):

视图层(add.jsp, delete.jsp, edit.jsp, list.jsp):

add.jsp:

delete.jsp

edit.jsp

list.jsp

控制器层(addStudent.jsp, deleteStudent.jsp, editStudent.jsp, StudentController.java):

addStudent.jsp

deleteStudent.jsp

editStudent.jsp

StudentController.java


模型层(Student.java):

public class Student {
    private int id;
    private String name;
    private String gender;
    private String birthday;
    private String major;

    public int getId() { return id; }
    public void setId(int id) { this.id = id; }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public String getGender() { return gender; }
    public void setGender(String gender) { this.gender = gender; }

    public String getBirthday() { return birthday; }
    public void setBirthday(String birthday) { this.birthday = birthday; }

    public String getMajor() { return major; }
    public void setMajor(String major) { this.major = major; }
}

数据访问对象层(StudentDAO.java):

public class StudentDAO {
    private Connection conn;

    public StudentDAO() {
        conn = Database.getConnection();
    }

    public void addStudent(Student student) {
        try {
            PreparedStatement ps = conn.prepareStatement("INSERT INTO student(name, gender, birthday, major) VALUES (?, ?, ?, ?)");
            ps.setString(1, student.getName());
            ps.setString(2, student.getGender());
            ps.setString(3, student.getBirthday());
            ps.setString(4, student.getMajor());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteStudent(int id) {
        try {
            PreparedStatement ps = conn.prepareStatement("DELETE FROM student WHERE id=?");
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void updateStudent(Student student) {
        try {
            PreparedStatement ps = conn.prepareStatement("UPDATE student SET name=?, gender=?, birthday=?, major=? WHERE id=?");
            ps.setString(1, student.getName());
            ps.setString(2, student.getGender());
            ps.setString(3, student.getBirthday());
            ps.setString(4, student.getMajor());
            ps.setInt(5, student.getId());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List getAllStudents() {
        List students = new ArrayList();
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM student");
            while (rs.next()) {
                Student student = new Student();
                student.setId(rs.getInt("id"));
                student.setName(rs.getString("name"));
                student.setGender(rs.getString("gender"));
                student.setBirthday(rs.getString("birthday"));
                student.setMajor(rs.getString("major"));
                students.add(student);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return students;
    }

    public Student getStudentById(int id) {
        Student student = new Student();
        try {
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM student WHERE id=?");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                student.setId(rs.getInt("id"));
                student.setName(rs.getString("name"));
                student.setGender(rs.getString("gender"));
                student.setBirthday(rs.getString("birthday"));
                student.setMajor(rs.getString("major"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return student;
    }
}

视图层(add.jsp, delete.jsp, edit.jsp, list.jsp):

add.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>



    
    Add Student


    
Name:
Gender: Male Female
Birthday:
Major:

delete.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>



    
    Delete Student


    
ID:

edit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>



    
    Edit Student


    
ID:
Name:
Gender: Male Female
Birthday:
Major:

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>



    
    List Students


    
        <%
            StudentDAO studentDAO = new StudentDAO();
            List students = studentDAO.getAllStudents();
            for (Student student : students) {
                out.println("");
                out.println("");
                out.println("");
                out.println("");
                out.println("");
                out.println("");
                out.println("");
                out.println("");
                out.println("");
            }
        %>
    
ID Name Gender Birthday Major Edit Delete
" + student.getId() + "" + student.getName() + "" + student.getGender() + "" + student.getBirthday() + "" + student.getMajor() + "EditDelete

Add Student

控制器层(addStudent.jsp, deleteStudent.jsp, editStudent.jsp, StudentController.java):

addStudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    Student student = new Student();
    student.setName(request.getParameter("name"));
    student.setGender(request.getParameter("gender"));
    student.setBirthday(request.getParameter("birthday"));
    student.setMajor(request.getParameter("major"));

    StudentDAO studentDAO = new StudentDAO();
    studentDAO.addStudent(student);

    response.sendRedirect("list.jsp");
%>

deleteStudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    int id = Integer.parseInt(request.getParameter("id"));

    StudentDAO studentDAO = new StudentDAO();
    studentDAO.deleteStudent(id);

    response.sendRedirect("list.jsp");
%>

editStudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    Student student = new Student();
    student.setId(Integer.parseInt(request.getParameter("id")));
    student.setName(request.getParameter("name"));
    student.setGender(request.getParameter("gender"));
    student.setBirthday(request.getParameter("birthday"));
    student.setMajor(request.getParameter("major"));

    StudentDAO studentDAO = new StudentDAO();
    studentDAO.updateStudent(student);

    response.sendRedirect("list.jsp");
%>

StudentController.java

@WebServlet("/")
public class StudentController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private StudentDAO studentDAO;
    
    public void init() {
        studentDAO = new StudentDAO();
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getServletPath();
        switch (action) {
            case "/new":
                showAddForm(request, response);
                break;
            case "/insert":
                addStudent(request, response);
                break;
            case "/delete":
                deleteStudent(request, response);
                break;
            case "/edit":
                showEditForm(request, response);
                break;
            case "/update":
                updateStudent(request, response);
                break;
            default:
                listStudents(request, response);
                break;
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    
    private void listStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List students = studentDAO.getAllStudents();
        request.setAttribute("students", students);
        RequestDispatcher dispatcher = request.getRequestDispatcher("list.jsp");
        dispatcher.forward(request, response);
    }
    
    private void showAddForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("add.jsp");
        dispatcher.forward(request, response);
    }
    
    private void addStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Student student = new Student();
        student.setName(request.getParameter("name"));
        student.setGender(request.getParameter("gender"));
        student.setBirthday(request.getParameter("birthday"));
        student.setMajor(request.getParameter("major"));

        studentDAO.addStudent(student);
        response.sendRedirect("list");
    }
    
    private void deleteStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));

        studentDAO.deleteStudent(id);
        response.sendRedirect("list");
    }
    
    private void showEditForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Student student = studentDAO.getStudentById(id);
        RequestDispatcher dispatcher = request.getRequestDispatcher("edit.jsp");
        request.setAttribute("student", student);
        dispatcher.forward(request, response);
    }
    
    private void updateStudent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Student student = new Student();
        student.setId(Integer.parseInt(request.getParameter("id")));
        student.setName(request.getParameter("name"));
        student.setGender(request.getParameter("gender"));
        student.setBirthday(request.getParameter("birthday"));
        student.setMajor(request.getParameter("major"));

        studentDAO.updateStudent(student);
        response.sendRedirect("list");
    }
}

注:如果使用Maven进行工程管理,则需要在pom.xml文件中添加相应的依赖包

你可能感兴趣的:(java,mvc,servlet)