MVC in JSP (Architecture)
โก Smart Summary
MVC in JSP separates application concerns into Model, View, and Controller layers, where JSP renders the View, a Servlet acts as the Controller, and JavaBeans encapsulate the Model for clean, testable, maintainable web applications.

What is MVC?
MVC stands for Model-View-Controller. It is a software design pattern that separates an application into three interconnected layers: business logic, presentation, and data. In MVC:
- M stands for Model
- V stands for View
- C stands for Controller
MVC is a systematic way to build applications where the flow starts at the View layer. The request is raised by the user, processed in the Controller layer, and sent to the Model layer to insert or fetch data. The Model then returns a success or failure response that the View renders for the user. The MVC architecture diagram is represented below:

Why Use MVC Architecture in JSP?
Before jumping into how MVC works in JSP, it helps to understand why developers adopt this pattern. Mixing business logic, database calls, and HTML inside a single JSP page quickly produces unmaintainable spaghetti code. MVC fixes that by giving each concern its own layer:
- Code becomes easier to read because each file has a single responsibility.
- Teams can work on the View, Controller, and Model in parallel without conflicts.
- Unit tests target the Model directly, without touching JSP rendering.
- Swapping a JSP View for a JSON response or a different template engine is straightforward.
How MVC Works in JSP
In a JSP-based MVC application, each layer maps to a specific Java technology. JSP files render the View, a Servlet acts as the Controller, and JavaBeans encapsulate the Model. The sections below explain the responsibilities of each layer.
Model Layer
- This is the data layer which contains the business logic of the system.
- It consists of all the data of the application.
- It also represents the state of the application.
- It is built from JavaBeans or POJOs that connect to the database.
- The Controller communicates with the Model to fetch data and forwards it to the View layer.
- The Model connects to the database and persists data into it.
View Layer
- This is the presentation layer of the application.
- It consists of HTML, JSP, CSS, and JavaScript.
- It normally presents the user interface of the application.
- It displays the data which is fetched from the Controller, which in turn fetches data from Model layer classes.
- The View layer renders data on the UI of the application without containing business logic.
Controller Layer
- It acts as an interface between View and Model.
- It intercepts all the requests which are coming from the View layer.
- It receives the requests from the View layer, processes them, and performs validation on the request.
- The request is forwarded to the Model layer for data processing. Once the request is processed, the Model sends the result back to the Controller with the required information, which the View then displays.
Advantages of MVC Architecture
The advantages of MVC are:
- Easy to maintain
- Easy to extend
- Easy to test
- Navigation control is centralized
- Loose coupling between layers makes refactoring safer
- Parallel development by multiple team members is possible
Example of JSP Application Design with MVC Architecture
In this example, we are going to show how to use MVC architecture in a JSP application.
- We are taking the example of a form with two variables, “email” and “password,” which represent our View layer.
- Once the user enters email and password and clicks Submit, the action is passed to Mvc_servlet where email and password are passed.
- Mvc_servlet is the Controller layer. Here in Mvc_servlet, the request is sent to the bean object which acts as the Model layer.
- The email and password values are set into the bean and stored for further use.
- From the bean, the value is fetched and shown in the View layer.
Mvc_example.jsp (View)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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>MVC Guru Example</title> </head> <body> <form action="Mvc_servlet" method="POST"> Email: <input type="text" name="email"> <br /> Password: <input type="text" name="password" /> <input type="submit" value="Submit" /> </form> </body> </html>
Explanation of the code:
View Layer:
Code Line 10-15: Here we are taking a form which has two fields as parameters, “email” and “password,” and this request needs to be forwarded to a controller, Mvc_servlet.java, which is passed in the action attribute. The HTTP method through which it is passed is POST.
Mvc_servlet.java (Controller)
package demotest; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Mvc_servlet */ public class Mvc_servlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Mvc_servlet() { super(); // TODO Auto-generated constructor stub } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String email=request.getParameter("email"); String password=request.getParameter("password"); TestBean testobj = new TestBean(); testobj.setEmail(email); testobj.setPassword(password); request.setAttribute("gurubean",testobj); RequestDispatcher rd=request.getRequestDispatcher("mvc_success.jsp"); rd.forward(request, response); } }
Explanation of the code:
Controller Layer:
Code Line 14: Mvc_servlet is extending HttpServlet.
Code Line 26: As the method used is POST, the request enters the doPost method of the Servlet, which processes the request and saves it into the bean object as testobj.
Code Line 34: Using the request object, we are setting the attribute “gurubean” which is assigned the value of testobj.
Code Line 35: Here we are using the RequestDispatcher object to forward the success message to mvc_success.jsp.
TestBean.java (Model)
package demotest; import java.io.Serializable; public class TestBean implements Serializable{ public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private String email="null"; private String password="null"; }
Explanation of the code:
Model Layer:
Code Line 7-17: It contains the getters and setters of email and password which are members of the TestBean class.
Code Line 19-20: It defines the members email and password of String type in the bean class.
Mvc_success.jsp (Result View)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@page import="demotest.TestBean"%> <!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>Guru Success</title> </head> <body> <% TestBean testguru=(TestBean)request.getAttribute("gurubean"); out.print("Welcome, "+testguru.getEmail()); %> </body> </html>
Explanation of the code:
Code Line 12: We are retrieving the attribute using the request object that was set in the doPost method of the Servlet.
Code Line 13: We are printing the welcome message and email id which has been saved in the bean object.
Output
When you execute the above code, you get the following output:
When you open mvc_example.jsp, you get the form with email and password fields and the Submit button.
Once you enter email and password into the form and click Submit:
After clicking Submit, the output is shown as below:
When you enter email and password on the screen and click Submit, the details are saved in TestBean. From the TestBean, they are fetched on the next screen to display the success message. JSP plays the role of presenting the data and providing the View, while the Servlet acts as the Controller and the JavaBean serves as the Model. The main business logic is contained in the Model layer.


