MVC Architecture in Java Explained
MVC (Model-View-Controller) is an architectural pattern that splits a web application into three responsibilities so that each part has one clear job. The Model holds data and business rules, the View displays that data to the user, and the Controller receives requests and coordinates between the two. Separating these concerns keeps code organised, testable, and easy to change — which is why nearly every Java web framework, including Spring MVC, is built around it.
What each layer does
- Model — your domain objects and the logic that operates on them (a
Student, aCourse, the service that calculates fees). The model knows nothing about the web. - View — the presentation layer that renders output. In Java this might be a JSP, a Thymeleaf template, or a JSON response.
- Controller — the traffic director. It reads the incoming request, calls the right model/service methods, and picks which view should render the result.
The golden rule: the view never talks directly to the database, and the model never knows about HTTP. The controller is the only layer that touches the web request and decides what happens next.
How a request flows through MVC
- A browser sends a request to a URL.
- The controller mapped to that URL runs. It validates input and calls a service.
- The service uses the model to fetch or change data.
- The controller puts the result into a model object and selects a view.
- The view renders the response (HTML or JSON) back to the browser.
This is the same flow whether you write raw servlets + JSP or use Spring MVC — Spring just automates the wiring.
A Spring MVC controller
Here is a controller that returns a list of courses to a view. Notice how thin it is — logic lives in the service, not here:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Controller // marks this as an MVC controller that returns view names
public class CourseController {
private final CourseService courseService;
// Spring injects the service (the Model layer's gateway)
public CourseController(CourseService courseService) {
this.courseService = courseService;
}
// Handles GET /courses
@GetMapping("/courses")
public String listCourses(Model model) {
// Ask the service (business logic) for data
model.addAttribute("courses", courseService.findAll());
// Return the VIEW name; Spring resolves it to a template
return "courses";
}
// Handles GET /courses/42 — captures the id from the URL
@GetMapping("/courses/{id}")
public String courseDetail(@PathVariable long id, Model model) {
model.addAttribute("course", courseService.findById(id));
return "course-detail";
}
}
The Model object here is a small carrier that hands data to the view — not to be confused with the domain "Model" layer, which is your Course and CourseService. The controller stays focused on coordination only.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesWhy MVC matters
Because each layer is independent, you can:
- Swap the view (JSP today, a React frontend tomorrow) without touching business logic.
- Unit-test the service and model with no web server running.
- Let different people work on different layers without stepping on each other.
This separation is what lets a small app grow into a large one without turning into spaghetti.
Common mistakes
- Putting business logic in the controller. Controllers should be thin — read input, call a service, return a view. Fat controllers become untestable.
- Letting the view query the database. Views should only display data the controller already gathered, never fetch it themselves.
- Mixing the two meanings of "model". The domain model (your entities) and Spring's
Modelcarrier object are different things; do not conflate them. - Skipping the service layer. Calling repositories directly from controllers couples web concerns to data access. A service layer keeps logic reusable.
- One giant controller for everything. Group controllers by feature (courses, students, fees) for clarity.
FAQ
Is MVC only for web apps? The pattern appears in desktop and mobile apps too, but in Java it is most associated with web frameworks like Spring MVC.
Does MVC apply to REST APIs? Yes. In a REST API the "view" is simply the JSON serialization of your data instead of an HTML page.
Keep learning
MVC pulls together servlets and JSP into a clean structure, and it is the heart of the Spring Framework. Explore more in the Advanced Java hub.
Want to architect Java apps the professional way? Join the waitlist for the Advanced Java course at Infoplanet, Jalgaon.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Advanced Java / Backend Roadmap
A clear, ordered roadmap for learning Advanced Java and backend development — from core Java through servlets, Spring Boot, REST APIs, and deployment.
Deploying a Java Web App
A practical guide to deploying a Spring Boot app: build an executable JAR, run it on a server, containerise with Docker, and check production essentials.
Hibernate & JPA Basics
Hibernate is the leading JPA implementation that maps Java objects to database tables. This guide explains ORM, entities, and the Jakarta Persistence annotations.
