Skip to content

Commit bcc3b6e

Browse files
haseebahmad11pedja4
authored andcommitted
BAEL-1343 MVC Architecture with Servlets and JSP code (eugenp#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
1 parent 3d4a179 commit bcc3b6e

7 files changed

Lines changed: 176 additions & 1 deletion

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.controller;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.RequestDispatcher;
6+
import javax.servlet.ServletException;
7+
import javax.servlet.annotation.WebServlet;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
12+
import com.baeldung.service.StudentService;
13+
14+
/**
15+
*
16+
* @author haseeb
17+
*
18+
*/
19+
@WebServlet(name = "StudentServlet", urlPatterns = "/student-record")
20+
public class StudentServlet extends HttpServlet {
21+
22+
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
23+
StudentService studentService = new StudentService();
24+
String studentID = request.getParameter("id");
25+
if (studentID != null) {
26+
int id = Integer.parseInt(studentID);
27+
request.setAttribute("studentRecord", studentService.getStudent(id));
28+
}
29+
30+
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/student-record.jsp");
31+
dispatcher.forward(request, response);
32+
}
33+
34+
@Override
35+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
36+
processRequest(request, response);
37+
}
38+
39+
@Override
40+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
41+
processRequest(request, response);
42+
}
43+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.model;
2+
3+
/**
4+
*
5+
* @author haseeb
6+
*
7+
*/
8+
public class Student {
9+
10+
private int id;
11+
private String firstName;
12+
private String lastName;
13+
14+
public Student(int id, String firstName, String lastName) {
15+
super();
16+
this.id = id;
17+
this.firstName = firstName;
18+
this.lastName = lastName;
19+
}
20+
21+
/**
22+
* @return the id
23+
*/
24+
public int getId() {
25+
return id;
26+
}
27+
28+
/**
29+
* @param id the id to set
30+
*/
31+
public void setId(int id) {
32+
this.id = id;
33+
}
34+
35+
/**
36+
* @return the firstName
37+
*/
38+
public String getFirstName() {
39+
return firstName;
40+
}
41+
42+
/**
43+
* @param firstName the firstName to set
44+
*/
45+
public void setFirstName(String firstName) {
46+
this.firstName = firstName;
47+
}
48+
49+
/**
50+
* @return the lastName
51+
*/
52+
public String getLastName() {
53+
return lastName;
54+
}
55+
56+
/**
57+
* @param lastName the lastName to set
58+
*/
59+
public void setLastName(String lastName) {
60+
this.lastName = lastName;
61+
}
62+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.service;
2+
3+
import com.baeldung.model.Student;
4+
5+
/**
6+
*
7+
* @author haseeb
8+
*
9+
*/
10+
public class StudentService {
11+
12+
/**
13+
*
14+
* @param id
15+
* @return
16+
*/
17+
public Student getStudent(int id) {
18+
19+
Student student = null;
20+
21+
switch (id) {
22+
case 1:
23+
student = new Student(1, "John", "Doe");
24+
break;
25+
case 2:
26+
student = new Student(2, "Jane", "Goodall");
27+
break;
28+
case 3:
29+
student = new Student(3, "Max", "Born");
30+
break;
31+
}
32+
return student;
33+
}
34+
}

‎javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
2525
response.setHeader("Test", "Success");
2626
response.setHeader("BMI", String.valueOf(bmi));
2727

28-
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
28+
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp");
2929
dispatcher.forward(request, response);
3030
} catch (Exception e) {
3131

javax-servlets/web/index.jsp renamed to javax-servlets/src/main/webapp/WEB-INF/jsp/index.jsp

File renamed without changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
4+
<%@ page import="com.baeldung.model.Student"%>
5+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
6+
<html>
7+
<head>
8+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
9+
<title>Student Record</title>
10+
</head>
11+
<body>
12+
<%
13+
if (request.getAttribute("studentRecord") != null) {
14+
Student student = (Student) request.getAttribute("studentRecord");
15+
%>
16+
17+
<h1>Student Record</h1>
18+
<div>
19+
ID:
20+
<%=student.getId()%></div>
21+
<div>
22+
First Name:
23+
<%=student.getFirstName()%></div>
24+
<div>
25+
Last Name:
26+
<%=student.getLastName()%></div>
27+
28+
<%
29+
} else {
30+
%>
31+
<h1>No student record found.</h1>
32+
<%
33+
}
34+
%>
35+
</body>
36+
</html>
File renamed without changes.

0 commit comments

Comments
 (0)