BAEL-1343 MVC Architecture with Servlets and JSP code (#3624)

* BAEL-1343 MVC Architecture with Servlets and JSP code

* BAEL-1343 code refactoring

* BAEL-1343 code refactoring

* BAEL-1343 updated code formatting

* BAEL-1343 code refactoring
This commit is contained in:
haseebahmad11 2018-02-14 05:49:46 +05:00 committed by Predrag Maric
parent 3d4a179ca3
commit bcc3b6ed95
7 changed files with 176 additions and 1 deletions

View File

@ -0,0 +1,43 @@
package com.baeldung.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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();
String studentID = request.getParameter("id");
if (studentID != null) {
int id = Integer.parseInt(studentID);
request.setAttribute("studentRecord", studentService.getStudent(id));
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/student-record.jsp");
dispatcher.forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
}

View File

@ -0,0 +1,62 @@
package com.baeldung.model;
/**
*
* @author haseeb
*
*/
public class Student {
private int id;
private String firstName;
private String lastName;
public Student(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
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

@ -0,0 +1,34 @@
package com.baeldung.service;
import com.baeldung.model.Student;
/**
*
* @author haseeb
*
*/
public class StudentService {
/**
*
* @param id
* @return
*/
public Student getStudent(int id) {
Student student = null;
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;
}
return student;
}
}

View File

@ -25,7 +25,7 @@ public class FormServlet extends HttpServlet {
response.setHeader("Test", "Success");
response.setHeader("BMI", String.valueOf(bmi));
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp");
dispatcher.forward(request, response);
} catch (Exception e) {

View File

@ -0,0 +1,36 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.baeldung.model.Student"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Student Record</title>
</head>
<body>
<%
if (request.getAttribute("studentRecord") != null) {
Student student = (Student) request.getAttribute("studentRecord");
%>
<h1>Student Record</h1>
<div>
ID:
<%=student.getId()%></div>
<div>
First Name:
<%=student.getFirstName()%></div>
<div>
Last Name:
<%=student.getLastName()%></div>
<%
} else {
%>
<h1>No student record found.</h1>
<%
}
%>
</body>
</html>