|
1 | 1 | package com.baeldung.controller; |
2 | 2 |
|
3 | | -import java.io.IOException; |
| 3 | +import com.baeldung.service.StudentService; |
4 | 4 |
|
5 | 5 | import javax.servlet.RequestDispatcher; |
6 | 6 | import javax.servlet.ServletException; |
7 | 7 | import javax.servlet.annotation.WebServlet; |
8 | 8 | import javax.servlet.http.HttpServlet; |
9 | 9 | import javax.servlet.http.HttpServletRequest; |
10 | 10 | import javax.servlet.http.HttpServletResponse; |
| 11 | +import java.io.IOException; |
11 | 12 |
|
12 | | -import com.baeldung.service.StudentService; |
13 | | - |
14 | | -/** |
15 | | - * |
16 | | - * @author haseeb |
17 | | - * |
18 | | - */ |
19 | 13 | @WebServlet(name = "StudentServlet", urlPatterns = "/student-record") |
20 | 14 | public class StudentServlet extends HttpServlet { |
21 | 15 |
|
22 | | - protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
23 | | - StudentService studentService = new StudentService(); |
| 16 | + private final StudentService studentService = new StudentService(); |
| 17 | + |
| 18 | + private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
24 | 19 | String studentID = request.getParameter("id"); |
25 | 20 | if (studentID != null) { |
26 | 21 | int id = Integer.parseInt(studentID); |
27 | | - request.setAttribute("studentRecord", studentService.getStudent(id)); |
| 22 | + studentService.getStudent(id) |
| 23 | + .ifPresent(s -> request.setAttribute("studentRecord", s)); |
28 | 24 | } |
29 | 25 |
|
30 | 26 | RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/student-record.jsp"); |
|
0 commit comments