JSP Refactor (#3689)

This commit is contained in:
Grzegorz Piwowarek 2018-02-19 01:43:23 +01:00 committed by Predrag Maric
parent fe97d98c30
commit 0cb1447797
4 changed files with 20 additions and 64 deletions

View File

@ -1,6 +1,6 @@
package com.baeldung.controller; package com.baeldung.controller;
import java.io.IOException; import com.baeldung.service.StudentService;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@ -8,23 +8,19 @@ import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.baeldung.service.StudentService;
/**
*
* @author haseeb
*
*/
@WebServlet(name = "StudentServlet", urlPatterns = "/student-record") @WebServlet(name = "StudentServlet", urlPatterns = "/student-record")
public class StudentServlet extends HttpServlet { public class StudentServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { private final StudentService studentService = new StudentService();
StudentService studentService = new StudentService();
private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String studentID = request.getParameter("id"); String studentID = request.getParameter("id");
if (studentID != null) { if (studentID != null) {
int id = Integer.parseInt(studentID); 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"); RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/student-record.jsp");

View File

@ -1,10 +1,5 @@
package com.baeldung.model; package com.baeldung.model;
/**
*
* @author haseeb
*
*/
public class Student { public class Student {
private int id; private int id;
@ -18,44 +13,26 @@ public class Student {
this.lastName = lastName; this.lastName = lastName;
} }
/**
* @return the id
*/
public int getId() { public int getId() {
return id; return id;
} }
/**
* @param id the id to set
*/
public void setId(int id) { public void setId(int id) {
this.id = id; this.id = id;
} }
/**
* @return the firstName
*/
public String getFirstName() { public String getFirstName() {
return firstName; return firstName;
} }
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) { public void setFirstName(String firstName) {
this.firstName = firstName; this.firstName = firstName;
} }
/**
* @return the lastName
*/
public String getLastName() { public String getLastName() {
return lastName; return lastName;
} }
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) { public void setLastName(String lastName) {
this.lastName = lastName; this.lastName = lastName;
} }

View File

@ -2,33 +2,20 @@ package com.baeldung.service;
import com.baeldung.model.Student; import com.baeldung.model.Student;
/** import java.util.Optional;
*
* @author haseeb
*
*/
public class StudentService { public class StudentService {
/** public Optional<Student> getStudent(int id) {
*
* @param id
* @return
*/
public Student getStudent(int id) {
Student student = null;
switch (id) { switch (id) {
case 1: case 1:
student = new Student(1, "John", "Doe"); return Optional.of(new Student(1, "John", "Doe"));
break; case 2:
case 2: return Optional.of(new Student(2, "Jane", "Goodall"));
student = new Student(2, "Jane", "Goodall"); case 3:
break; return Optional.of(new Student(3, "Max", "Born"));
case 3: default:
student = new Student(3, "Max", "Born"); return Optional.empty();
break;
} }
return student;
} }
} }

View File

@ -1,7 +1,6 @@
package com.baeldung.servlets; package com.baeldung.servlets;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -13,7 +12,7 @@ public class FormServlet extends HttpServlet {
@Override @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws IOException {
String height = request.getParameter("height"); String height = request.getParameter("height");
String weight = request.getParameter("weight"); String weight = request.getParameter("weight");
@ -28,20 +27,17 @@ public class FormServlet extends HttpServlet {
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp"); RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp");
dispatcher.forward(request, response); dispatcher.forward(request, response);
} catch (Exception e) { } catch (Exception e) {
response.sendRedirect("index.jsp"); response.sendRedirect("index.jsp");
} }
} }
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) protected void doGet(HttpServletRequest request, HttpServletResponse response) {
throws ServletException, IOException {
// do something else here // do something else here
} }
private Double calculateBMI(Double weight, Double height) { private Double calculateBMI(Double weight, Double height) {
return weight / (height * height); return weight / (height * height);
} }
} }