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;
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");

View File

@ -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;
}

View File

@ -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<Student> getStudent(int id) {
switch (id) {
case 1:
student = new Student(1, "John", "Doe");
break;
return Optional.of(new Student(1, "John", "Doe"));
case 2:
student = new Student(2, "Jane", "Goodall");
break;
return Optional.of(new Student(2, "Jane", "Goodall"));
case 3:
student = new Student(3, "Max", "Born");
break;
return Optional.of(new Student(3, "Max", "Born"));
default:
return Optional.empty();
}
return student;
}
}

View File

@ -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);
}
}