| 1 | package controller;
|
|---|
| 2 |
|
|---|
| 3 | import model.Faculty;
|
|---|
| 4 | import model.Student;
|
|---|
| 5 | import model.Subject;
|
|---|
| 6 | import service.FacultyService;
|
|---|
| 7 | import service.ProfessorService;
|
|---|
| 8 | import service.StudentService;
|
|---|
| 9 | import service.SubjectService;
|
|---|
| 10 | import service.impl.FacultyServiceImpl;
|
|---|
| 11 | import service.impl.ProfessorServiceImpl;
|
|---|
| 12 | import service.impl.StudentServiceImpl;
|
|---|
| 13 | import service.impl.SubjectServiceImpl;
|
|---|
| 14 |
|
|---|
| 15 | import javax.servlet.ServletException;
|
|---|
| 16 | import javax.servlet.annotation.WebServlet;
|
|---|
| 17 | import javax.servlet.http.HttpServlet;
|
|---|
| 18 | import javax.servlet.http.HttpServletRequest;
|
|---|
| 19 | import javax.servlet.http.HttpServletResponse;
|
|---|
| 20 | import java.io.IOException;
|
|---|
| 21 | import java.sql.SQLException;
|
|---|
| 22 | import java.util.*;
|
|---|
| 23 | import java.util.stream.Collectors;
|
|---|
| 24 |
|
|---|
| 25 | //@WebServlet("/student/*")
|
|---|
| 26 | public class StudentServlet extends HttpServlet {
|
|---|
| 27 |
|
|---|
| 28 | private static final long serialVersionUID = 1L;
|
|---|
| 29 |
|
|---|
| 30 | private final StudentService studentService = new StudentServiceImpl();
|
|---|
| 31 | private final FacultyService facultyService = new FacultyServiceImpl();
|
|---|
| 32 | private final ProfessorService professorService = new ProfessorServiceImpl();
|
|---|
| 33 | private final SubjectService subjectService = new SubjectServiceImpl();
|
|---|
| 34 |
|
|---|
| 35 | @Override
|
|---|
| 36 | protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 37 | throws ServletException, IOException {
|
|---|
| 38 | doGet(request, response);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | @Override
|
|---|
| 42 | protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 43 | throws ServletException, IOException {
|
|---|
| 44 |
|
|---|
| 45 | String action = request.getPathInfo();
|
|---|
| 46 | if (action == null || action.equals("/")) {
|
|---|
| 47 | action = "/list";
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | try {
|
|---|
| 51 | switch (action) {
|
|---|
| 52 | case "/new":
|
|---|
| 53 | showNewForm(request, response);
|
|---|
| 54 | break;
|
|---|
| 55 | case "/insert":
|
|---|
| 56 | insertStudent(request, response);
|
|---|
| 57 | break;
|
|---|
| 58 | case "/delete":
|
|---|
| 59 | deleteStudent(request, response);
|
|---|
| 60 | break;
|
|---|
| 61 | case "/edit":
|
|---|
| 62 | showEditForm(request, response);
|
|---|
| 63 | break;
|
|---|
| 64 | case "/update":
|
|---|
| 65 | updateStudent(request, response);
|
|---|
| 66 | break;
|
|---|
| 67 | case "/list":
|
|---|
| 68 | default:
|
|---|
| 69 | listStudent(request, response);
|
|---|
| 70 | break;
|
|---|
| 71 | }
|
|---|
| 72 | } catch (SQLException ex) {
|
|---|
| 73 | throw new ServletException("Database error: " + ex.getMessage(), ex);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | private void listStudent(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 78 | throws SQLException, IOException, ServletException {
|
|---|
| 79 | List<Student> listStudent = studentService.getAllStudent();
|
|---|
| 80 | request.setAttribute("listStudent", listStudent);
|
|---|
| 81 | request.getRequestDispatcher("/pages/student-list.jsp").forward(request, response);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | private void showNewForm(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 85 | throws ServletException, IOException {
|
|---|
| 86 | try {
|
|---|
| 87 | request.setAttribute("faculties", facultyService.getAllFaculty());
|
|---|
| 88 | request.setAttribute("allSubjects", subjectService.getAllSubjects());
|
|---|
| 89 | request.getRequestDispatcher("/pages/student-form.jsp").forward(request, response);
|
|---|
| 90 | } catch (SQLException e) {
|
|---|
| 91 | response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | private void showEditForm(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 96 | throws SQLException, ServletException, IOException {
|
|---|
| 97 | Long id = Long.parseLong(request.getParameter("id"));
|
|---|
| 98 | Student existingStudent = studentService.getById(id);
|
|---|
| 99 |
|
|---|
| 100 | if (existingStudent == null) {
|
|---|
| 101 | response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
|---|
| 102 | return;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | request.setAttribute("student", existingStudent);
|
|---|
| 106 | request.setAttribute("faculties", facultyService.getAllFaculty());
|
|---|
| 107 | request.getRequestDispatcher("/pages/student-form.jsp").forward(request, response);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | private void insertStudent(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 111 | throws SQLException, IOException {
|
|---|
| 112 | Student student = parseStudentFromRequest(request);
|
|---|
| 113 | studentService.save(student);
|
|---|
| 114 | response.sendRedirect(request.getContextPath() + "/student/list");
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | private void updateStudent(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 118 | throws SQLException, IOException {
|
|---|
| 119 | Student student = parseStudentFromRequest(request);
|
|---|
| 120 | student.setId(Long.parseLong(request.getParameter("id")));
|
|---|
| 121 | studentService.update(student);
|
|---|
| 122 | response.sendRedirect(request.getContextPath() + "/student/list");
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | private void deleteStudent(HttpServletRequest request, HttpServletResponse response)
|
|---|
| 126 | throws SQLException, IOException {
|
|---|
| 127 | Long id = Long.parseLong(request.getParameter("id"));
|
|---|
| 128 | studentService.delete(id);
|
|---|
| 129 | response.sendRedirect(request.getContextPath() + "/student/list");
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | private Student parseStudentFromRequest(HttpServletRequest request) {
|
|---|
| 133 | String name = request.getParameter("name");
|
|---|
| 134 | String surname = request.getParameter("surname");
|
|---|
| 135 | String location = request.getParameter("location");
|
|---|
| 136 | int indeks = Integer.parseInt(request.getParameter("studentindex"));
|
|---|
| 137 | long facultyId = Long.parseLong(request.getParameter("facultyid"));
|
|---|
| 138 |
|
|---|
| 139 | return new Student(null, name, surname, location, indeks, facultyId);
|
|---|
| 140 | }
|
|---|
| 141 | } |
|---|