From 0cb1447797da3904a7df5fe7d1989b5afa5ce4ff Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Mon, 19 Feb 2018 01:43:23 +0100 Subject: [PATCH] JSP Refactor (#3689) --- .../baeldung/controller/StudentServlet.java | 18 ++++------ .../main/java/com/baeldung/model/Student.java | 23 ------------ .../com/baeldung/service/StudentService.java | 35 ++++++------------- .../com/baeldung/servlets/FormServlet.java | 8 ++--- 4 files changed, 20 insertions(+), 64 deletions(-) diff --git a/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java b/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java index 3c893eab1a..b1924198a4 100644 --- a/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java @@ -1,6 +1,6 @@ package com.baeldung.controller; -import java.io.IOException; +import com.baeldung.service.StudentService; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; @@ -8,23 +8,19 @@ 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 com.baeldung.service.StudentService; - -/** - * - * @author haseeb - * - */ @WebServlet(name = "StudentServlet", urlPatterns = "/student-record") public class StudentServlet extends HttpServlet { - protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - StudentService studentService = new StudentService(); + private final StudentService studentService = new StudentService(); + + private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String studentID = request.getParameter("id"); if (studentID != null) { int id = Integer.parseInt(studentID); - request.setAttribute("studentRecord", studentService.getStudent(id)); + studentService.getStudent(id) + .ifPresent(s -> request.setAttribute("studentRecord", s)); } RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/student-record.jsp"); diff --git a/javax-servlets/src/main/java/com/baeldung/model/Student.java b/javax-servlets/src/main/java/com/baeldung/model/Student.java index 55afe6bc2a..ce8a27375a 100644 --- a/javax-servlets/src/main/java/com/baeldung/model/Student.java +++ b/javax-servlets/src/main/java/com/baeldung/model/Student.java @@ -1,10 +1,5 @@ package com.baeldung.model; -/** - * - * @author haseeb - * - */ public class Student { private int id; @@ -18,44 +13,26 @@ public class Student { this.lastName = lastName; } - /** - * @return the id - */ public int getId() { return id; } - /** - * @param id the id to set - */ public void setId(int id) { this.id = id; } - /** - * @return the firstName - */ public String getFirstName() { return firstName; } - /** - * @param firstName the firstName to set - */ public void setFirstName(String firstName) { this.firstName = firstName; } - /** - * @return the lastName - */ public String getLastName() { return lastName; } - /** - * @param lastName the lastName to set - */ public void setLastName(String lastName) { this.lastName = lastName; } diff --git a/javax-servlets/src/main/java/com/baeldung/service/StudentService.java b/javax-servlets/src/main/java/com/baeldung/service/StudentService.java index 1666850d50..525d47683f 100644 --- a/javax-servlets/src/main/java/com/baeldung/service/StudentService.java +++ b/javax-servlets/src/main/java/com/baeldung/service/StudentService.java @@ -2,33 +2,20 @@ package com.baeldung.service; import com.baeldung.model.Student; -/** - * - * @author haseeb - * - */ +import java.util.Optional; + public class StudentService { - /** - * - * @param id - * @return - */ - public Student getStudent(int id) { - - Student student = null; - + public Optional getStudent(int id) { switch (id) { - case 1: - student = new Student(1, "John", "Doe"); - break; - case 2: - student = new Student(2, "Jane", "Goodall"); - break; - case 3: - student = new Student(3, "Max", "Born"); - break; + case 1: + return Optional.of(new Student(1, "John", "Doe")); + case 2: + return Optional.of(new Student(2, "Jane", "Goodall")); + case 3: + return Optional.of(new Student(3, "Max", "Born")); + default: + return Optional.empty(); } - return student; } } diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java index 04c5fec42d..c78129a9cf 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java @@ -1,7 +1,6 @@ package com.baeldung.servlets; import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -13,7 +12,7 @@ public class FormServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + throws IOException { String height = request.getParameter("height"); String weight = request.getParameter("weight"); @@ -28,20 +27,17 @@ public class FormServlet extends HttpServlet { RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp"); dispatcher.forward(request, response); } catch (Exception e) { - response.sendRedirect("index.jsp"); } } @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + protected void doGet(HttpServletRequest request, HttpServletResponse response) { // do something else here } private Double calculateBMI(Double weight, Double height) { - return weight / (height * height); } } \ No newline at end of file