16 - Mini MVC using Servlets

16.1 Overview

MVC stands for Model View Controller and it is a web design pattern. MVC divides the application into three interconnected part or components, so as to separate the code that create or manage the data from the ways the information is presented to the user. The three components are:

  • Model (M): This is the central component of MVC, directly managing the application's data, business logic and rules.
  • View (V): A view is an output representation of information, such as displaying information or reports to the user either as a text form or as charts.
  • Controller(C): The third part, the controller, accepts input and converts it to commands for the model or view. Based on the type of request, the controller sends the request to Models to perform business logic and then update the view based on the model’s output.

There are several MVC frameworks are available like Spring, Struts etc but in this chapter we will implement MVC framework using JSP and servlets.

16.2 MVC Implementation

To implement a web application based on MVC design pattern, we would use

  1. JSP as View because it is used to display the results to the users or we can JSP acts a presentation layer.
  2. Servlet will act as controller which will scan the request and response and based on the request or response,  will delegate the request or response to business classes.
  3. Java Beans and Business logic classes together will be used to represent Model.

Lets discuss any general flow.

  • User will invoke the website and will see a JSP page (View)
  • User submits the data using JSP
  • JSP will invoke a servlet
  • Servlet will invoke the classes having business logic
  • Business logic classes will perform the business logic and populate the bean classes.
  • Servlet will scan the results of business logic classes and based on that , forward the control to any of the jsp
  • JSP will display the data

In ideal situation, all the requests are forwarded to one and only one controller( servlet )  and based on the request, controller delegates the request to corresponding business classes.

Now we will develop a “Find Available Rooms in Hostel” flow with the help of JSP and servlet using MVC pattern.

User will select the hostel name from a dropdown list and all the available rooms in the selected hostel will be displayed as a result. In case all rooms are occupied, message will be displayed.

Ideally , available rooms in a hostel should be verified and pulled from database but for the sake of simplicity, we will return some dummy data from business classes.

a. Write a SelectHostel.jsp in WebContent ditectory

<!DOCTYPE html>
<html>
<head>
<title>Select Residence</title>
</head>
<body>
    <H4>Select the Residence to check availability of Rooms.</H4>
    <form name="selectResidenceForm" action="GetRoomsByResidenceServlet"
        method="POST">
        <table>
            <tr>
                <td>Select Residence ::</td>
                <td><select id="Residence" name="Residence">
                        <option>Residence 1</option>
                        <option>Residence 2</option>
                        <option>Residence 3</option>
                        <option>Residence 4</option>
                </select></td>
            </tr>
            <tr>
                <td><input type="submit" value=" Search " /></td>
            </tr>
        </table>
    </form>
</body>
</html>

b. Define a Java Bean class Room which will be used as Data transfer Object to transfer the data from one layer to another. Room class will have room attributes like number , residence code to which this room belongs. Floor number on which this room is located and the rent of room.

package com.servlet.tutorial;
public class Room {
    private String roomNumber;
    private String residenceCode;
    private String floorNumber;
    private int roomRent;
    public String getRoomNumber() {
        return roomNumber;
    }
    public void setRoomNumber(String roomNumber) {
        this.roomNumber = roomNumber;
    }
    public String getResidenceCode() {
        return residenceCode;
    }
    public void setResidenceCode(String residenceCode) {
        this.residenceCode = residenceCode;
    }
    public String getFloorNumber() {
        return floorNumber;
    }
    public void setFloorNumber(String floorNumber) {
        this.floorNumber = floorNumber;
    }
    public int getRoomRent() {
        return roomRent;
    }
    public void setRoomRent(int roomRent) {
        this.roomRent = roomRent;
    }
}
 

Like us on Facebook