Hello! Welcome to your sixth lesson on Java backend development.
In our last lesson, we learned about Servlet Filters and how they can intercept requests to perform cross-cutting tasks like logging or authentication. This was our first look at centralizing logic that applies to multiple parts of an application.
Today, we take that idea of centralization and structure to the next level. We'll explore the Model-View-Controller (MVC) design pattern, which provides a blueprint for organizing your entire web application. This lesson addresses the learning outcome: Explain the Model-View-Controller (MVC) design pattern in the context of web applications.
Understanding MVC is fundamental. It's the architectural foundation for most modern web frameworks, including Spring MVC, which we'll be diving into soon. Mastering this pattern will help you write code that is clean, organized, and much easier to maintain as your applications grow in complexity.
What is a Design Pattern?
Before we dive into MVC, let's quickly clarify what a "design pattern" is. In software engineering, a design pattern is a general, reusable solution to a commonly occurring problem within a given context. It's not a finished piece of code, but rather a template for how to structure your code to solve a problem effectively. MVC is one of the most famous and widely used design patterns in web development.
The Core Idea: Separation of Concerns
The primary goal of the MVC pattern is to separate an application into three interconnected components. This separation makes your code more modular, flexible, and easier to test.
Here are the three components:
- Model: This is the "brains" of the application. It represents the application's data and contains the business logic. The Model's job is to manage the state of the application, including reading from and writing to a database, and enforcing business rules. It knows nothing about the user interface.
- View: This is the presentation layer—what the user sees. Its job is to render the data provided by the Model into a user-friendly format, typically HTML. The View should contain minimal logic; its main role is display.
- Controller: This is the intermediary. It acts as the link between the user, the Model, and the View. The Controller receives user input (e.g., from an HTTP request), processes it, interacts with the Model to update or fetch data, and then selects the appropriate View to render the response.
The following article provides a clear, concise definition of these roles within a Java Servlet and JSP context.
The article 'MVC with Servlets and JSP' on Javamasterclass.com gives a great written overview of the MVC components and their roles.
Please read the first two sections, down to (but not including) 'Flow of MVC'. Focus on the definitions provided for Model, View, and Controller.
The MVC Flow in a Web Application
Now, let's visualize how these components work together to handle a user's request.

The typical sequence of events is as follows:
- Request: The user interacts with the application, sending an HTTP request to the server (e.g., by clicking a link or submitting a form).
- Controller: The Controller (a Servlet in our case) receives the request. It parses the request to understand what the user wants to do.
- Model Interaction: The Controller calls the Model to perform the required business logic. This might involve fetching data from a database, creating new data, or updating existing data.
- Data Return: The Model completes its task and returns any necessary data to the Controller.
- View Selection: The Controller, now holding the data from the Model, selects the appropriate View (a JSP file) to display the result. It forwards the request, along with the model data, to this View.
- Response: The View uses the data passed from the Controller to dynamically render an HTML page. This final response is sent back to the user's browser.
To see a detailed explanation of this flow and how it specifically maps to Java technologies, let's watch a video.
This video from Telusko provides a great visual and conceptual explanation of how Servlets, JSPs, and POJOs (Plain Old Java Objects) fit into the roles of Controller, View, and Model.
Watch the video from 03:24 to 09:33. Pay close attention to: How the Controller (Servlet) receives a request. What the Model is (a POJO that holds data). How the Controller passes the Model to the View (JSP). The introduction of the optional but recommended Service and DAO layers for better organization.
As the video mentioned, in a real-world application, the Model is often more than just a single data object. It usually consists of:
- Domain Objects (POJOs): Simple Java classes that represent the data (e.g., a
Userclass, aProductclass). - Service Layer: Classes that contain the core business logic (e.g.,
UserServicefor handling user registration). - Data Access Objects (DAO): Classes responsible for communicating with the database.
A Practical Example: Building a Login Application
Theory is great, but seeing MVC in action makes it click. The following video walks through the creation of a simple login application, building each component of the MVC pattern from scratch.
First, let's watch the initial segment where the plan is laid out.
JSPs and Servlets Tutorial 16 Part 1- Writing an MVC Application
The Java Brains channel has an excellent tutorial that walks through building a simple login application using the MVC pattern. This first part outlines the plan and assigns a role to each component.
Please watch from the beginning until 02:53. This section explains the goal and maps the login functionality to the MVC components: the Controller will be a Servlet, the business logic will be in a service class, the data will be a model object, and the UI will be JSP pages.
Now that you've seen the plan, let's watch the implementation. As you watch, try to mentally label each piece of code with its MVC role: is it part of the Model, the View, or the Controller?
JSPs and Servlets Tutorial 16 Part 1- Writing an MVC Application
Now, let's see how these components are actually built. The video continues by creating the JSP, the Servlet, and a simple business service.
Watch from 02:53 to 14:31. As you watch, identify: The login.jsp as the View. The LoginServlet as the Controller and notice how it retrieves request parameters. The LoginService as part of the Model, handling the business logic (even if it's a dummy implementation for this example).
This hands-on example should solidify how a request flows through the different Java components that make up an MVC architecture.
Test your understanding!
Imagine you're building a feature to display a user's profile page. Based on the MVC pattern, which component (Model, View, or Controller) would be responsible for each of the following tasks?
- Writing the SQL query to fetch user data from the database.
- Defining a
UserProfileclass with fields likeusername,email, andjoinDate. - Creating the HTML layout with placeholders like
<h1>Welcome, ${user.username}</h1>. - Receiving the request for
/user/profile?id=123, calling the data-fetching logic, and forwarding to the correct JSP page.
Show answer
- Model (specifically, a Data Access Object or DAO). The Model is responsible for all data interactions.
- Model. This class is a POJO that represents the application's data structure.
- View. The View is responsible for presentation and rendering the data into HTML. The
${user.username}syntax is typical of JSP Expression Language, used within a View. - Controller. The Controller handles the incoming request, orchestrates the interaction with the Model, and selects the View.
Key Takeaways
- MVC is a design pattern that promotes Separation of Concerns by dividing an application into three parts: Model, View, and Controller.
- Model: Manages the application's data and business logic. In Java, this is often implemented with POJOs, Service classes, and DAOs.
- View: Renders the user interface. In Java, this is typically done using JSP pages.
- Controller: Handles user requests, interacts with the Model, and selects the View. In Java, this role is filled by Servlets.
- This separation makes applications more organized, scalable, and easier to test and maintain.
What's Next?
We've now seen how to structure an application using the MVC pattern, with a different servlet potentially acting as the controller for each distinct action. However, as an application grows, managing dozens of servlets can become cumbersome.
In our next lesson, we will explore the Front Controller design pattern. This pattern evolves the MVC structure by introducing a single, centralized controller (one servlet) that handles all incoming requests. This Front Controller then delegates the work to other, more specialized components. This is a crucial step toward understanding the architecture of Spring, as Spring's DispatcherServlet is a powerful implementation of this very pattern.