source: src/main/java/controller/StudentServlet.java@ 1eb7a55

Last change on this file since 1eb7a55 was 1eb7a55, checked in by Elena Markovska <elena.elenamarkovska@…>, 11 days ago

Initial commit - Scholaris project code

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[1eb7a55]1package controller;
2
3import model.Faculty;
4import model.Student;
5import model.Subject;
6import service.FacultyService;
7import service.ProfessorService;
8import service.StudentService;
9import service.SubjectService;
10import service.impl.FacultyServiceImpl;
11import service.impl.ProfessorServiceImpl;
12import service.impl.StudentServiceImpl;
13import service.impl.SubjectServiceImpl;
14
15import javax.servlet.ServletException;
16import javax.servlet.annotation.WebServlet;
17import javax.servlet.http.HttpServlet;
18import javax.servlet.http.HttpServletRequest;
19import javax.servlet.http.HttpServletResponse;
20import java.io.IOException;
21import java.sql.SQLException;
22import java.util.*;
23import java.util.stream.Collectors;
24
25//@WebServlet("/student/*")
26public 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}
Note: See TracBrowser for help on using the repository browser.