Advertisement
Guest User

Untitled

a guest
Apr 15th, 2023
28
0
179 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. // simple tutorial i followed:
  2. // https://www.baeldung.com/mvc-servlet-jsp
  3.  
  4. // filename: Student (I renamed this to Student.java)
  5.  
  6. public class Student {
  7.     // Student is the Model Layer (other Model Layer is StudentService) of the MVC Pattern
  8.     private int id;
  9.     private String firstName;
  10.     private String lastName;
  11.  
  12.     // constructors, getters and setters go here
  13. }
  14.  
  15. // filename: StudentService (I reanamed this to StudentService.java)
  16.  
  17. public class StudentService {
  18.     // StudentService is the Model Layer (other Model Layer is Student) of the MVC Pattern
  19.     public Optional<Student> getStudent(int id){
  20.         switch(id) {
  21.             case 1:
  22.                 return Optional.of(new Student(1, "John", "Doe"));
  23.             case 2:
  24.                 return Optional.of(new Student(2, "Jane", "Goodall"));
  25.             case 3:
  26.                 return Optional.of(new Student(3, "Max", "Born"));
  27.             default:
  28.                 return Optional.empty();
  29.         }
  30.     }
  31. }
  32.  
  33. // filename: StudentServlet (I renamed to StudentServlet.java)
  34.  
  35. @WebServlet(
  36.     name = "StudentServlet",
  37.     urlPatterns = "/student-record")
  38.  
  39. public class StudentServlet extends HttpServlet {
  40.     // StudentServlet is our Controller Layer of the MVC pattern
  41.     private StudentService studentService = new StudentService();
  42.  
  43.     private void processRequest(
  44.         HttpServletRequest request, HttpServletResponse response)
  45.         throws ServletException, IOException {
  46.             String studentID = request.getParameter("id");
  47.             if (studentID != null) {
  48.                 int id = Integer.parseInt(studentID);
  49.                 studentService.getStudent(id).ifPresent(s -> request.setAttribute("studentRecord", s));
  50.             }
  51.  
  52.             RequestDispatcher dispatcher = request.getRequestDispatcher(
  53.                 "/WEB-INF/jsp/student-record.jsp");
  54.                 dispatcher.forward(request, response);
  55.         }
  56.  
  57.     @Override
  58.     protected void doGet(
  59.         HttpServletRequest request, HttpServletresponse response)
  60.         throws ServletException, IOException {
  61.             processRequest(request, response);
  62.         }
  63.  
  64.     @Override
  65.     protected void doPost(
  66.         HttpServletRequest request, HttpServletResponse response)
  67.         throws ServletException, IOException {
  68.             processRequest(request, response);
  69.         }
  70.     }
  71.  
  72.  
  73. // filename: student-record.jsp
  74.  
  75. <html>
  76.     <head>
  77.         <title>Student Record</title>
  78.     </head>
  79.     <body>
  80.         <%
  81.             if (request.getAttribute("studentRecord") != null) {
  82.                 Student student = (Student) request.getAttribute("studentRecord");
  83.         %>
  84.  
  85.         <h1>Student Record</h1>
  86.         <div>ID: <%= student.getId()%></div>
  87.         <div> First Name: <%= student.getFirstName()%></div>
  88.         <div> Last Name: <%= student.getLastName()%></div>
  89.  
  90.         <%
  91.             } else {
  92.         %>
  93.  
  94.         <h1>No Student Record Found.</h1>
  95.  
  96.         <% } %>
  97.     </body>
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement