MVC and the Front Controller With Servlets and JSP
A Book model, a BookController servlet and a books.jsp view behind WEB-INF show MVC in plain Java. Then one servlet on /app/* becomes a front controller, the idea Spring's DispatcherServlet grew from.
What you will learn
- Split a page into a model class, a controller servlet and a JSP view
- Hide views under WEB-INF so only a controller can reach them
- Write a front controller on /app/* that picks a handler from getPathInfo
- Compare one servlet per URL with one servlet in front
Before you start
- Servlet Request Parameters, and Forward vs Redirect
- JSP scriptlets and expressions
MVC, model view controller, is one of the most common design patterns in web apps. The controller is a servlet that handles the request, the model is the data, and the view is a JSP that displays it. A request comes in, its URL picks the controller, and the controller hands the model to the view. After that we'll put one servlet in front of every page: the front controller.
The model is plain data
package org.studyeasy.web;
import java.util.List;
// The MODEL: plain data. No request, no response, no HTML.
public class Book {
private final String title;
private final String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
// the view reads the data through these getters
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
// sample data: a real app would read a database here
public static List<Book> all() {
return List.of(
new Book("The Quiet Harbor", "Emma Clarke"),
new Book("River of Stars", "James Turner"),
new Book("Code and Coffee", "Sophie Walker"));
}
}
Two fields, a constructor, two getters, and a static all() that returns three sample books (a real app would read a database here). Notice what's missing: no request, no response and no HTML. The model doesn't even know it's on the web.
The controller decides
package org.studyeasy.web;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
// The CONTROLLER: takes the request, gets the model, picks the view
@WebServlet("/books")
public class BookController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// ask the model for the data
List<Book> books = Book.all();
// hand the model to the view
req.setAttribute("books", books);
// forward on the server: the view under WEB-INF writes the page
req.getRequestDispatcher("/WEB-INF/views/books.jsp")
.forward(req, resp);
}
}
Every /books request comes here. Inside doGet the controller asks the model for data, puts the list on the request as books, and forwards to the view. There's no HTML in the controller. A URL that no servlet maps simply gets a 404.
The view, behind WEB-INF
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.List, org.studyeasy.web.Book" %>
<%-- The VIEW: shows the model it was handed, nothing more --%>
<% List<Book> books = (List<Book>) request.getAttribute("books"); %>
<html>
<body>
<h1>Books</h1>
<ul>
<% for (Book book : books) { %>
<li><%= book.getTitle() %> by <%= book.getAuthor() %></li>
<% } %>
</ul>
</body>
</html>
Open /books:
Books
* The Quiet Harbor by Emma Clarke
* River of Stars by James Turner
* Code and Coffee by Sophie Walker
The view reads the list the controller handed over and loops through it. It lives in WEB-INF/views, and that location matters: Tomcat never serves anything under WEB-INF to a browser. Try /WEB-INF/views/books.jsp directly and you get 404. The only way to this view is through a controller's forward.
Compare index.jsp, which sits in the web root. It's a JSP too, but it's public, so / opens it as the default page. Its two links go to books (the book controller) and app (the front controller).
A front controller
Now one controller for many URLs. The mapping /app/* uses the wildcard *, so anything after /app lands in this one class:
package org.studyeasy.web;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/app/*") // ONE servlet for every URL under /app
public class FrontController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String path = req.getPathInfo(); // the part after /app
String view;
if ("/books".equals(path)) {
view = listBooks(req);
} else if ("/home".equals(path)) {
view = home(req);
} else {
resp.sendError(404); // no handler for this path
return;
}
System.out.println("front controller: " + path + " -> " + view);
req.getRequestDispatcher("/WEB-INF/views/" + view + ".jsp")
.forward(req, resp);
}
private String listBooks(HttpServletRequest req) {
req.setAttribute("books", Book.all());
return "books";
}
private String home(HttpServletRequest req) {
req.setAttribute("count", Book.all().size());
return "home";
}
}
getPathInfo()gives the part after/app./bookscallslistBooks,/homecallshome.- Anything else, such as
/app/orders, getssendError(404). So does/appon its own. - There is one forward for every page; the view name goes into the path.
- Each handler puts the model on the request and returns a view name.
The second view:
<%@ page contentType="text/html;charset=UTF-8" %>
<%-- A second VIEW: the front controller forwards here for /app/home --%>
<html>
<body>
<h1>Library</h1>
<p>Books on the shelf: <%= request.getAttribute("count") %></p>
</body>
</html>
After visiting /app/books, /app/home and /app/orders, the log shows:
front controller: /books -> books
front controller: /home -> home
/app/home shows "Books on the shelf: 3". /app/orders has no handler, so the browser shows HTTP Status 404, with no view and no log line.
One servlet per URL or one in front?
| Servlet per URL | Front controller | |
|---|---|---|
| Classes | one per page | one class on /app/* |
| Add a page | a new servlet class | a new handler and a view |
| Shared steps (a login check) | copied into each servlet | written once |
| Where you meet it | small apps | inside web frameworks |
Spring's DispatcherServlet grew from this idea: one servlet in front, your controllers behind it.
Common mistakes
- Redirecting to a view,
resp.sendRedirect("/WEB-INF/views/books.jsp"). The browser itself now asks for a WEB-INF URL and gets 404. Views are reached by forward. - Making a getter private. The view can't read it:
/booksfails with HTTP 500, "The method getTitle() from the type Book is not visible". - Mapping
@WebServlet("/app")without/*. It only answers/appitself, and/app/booksis 404.
Quick recap
| Part | Here | Job |
|---|---|---|
| Model | Book | holds data, knows nothing about the web |
| Controller | BookController, FrontController | takes the request, gets the model, picks the view |
| View | WEB-INF/views/*.jsp | turns the model into HTML |
You don't need to memorise this syntax; an AI assistant writes it quickly. What you need is the flow, so you stay in control of the code.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Forms With JSP and Servlets: Handle the POST and Validate on the Server
Post a sign-up form straight to a JSP and anything gets accepted. Post it to a servlet instead: doPost checks name and email, forwards back with errors and the typed values, or on to the welcome page.
Mini App With Servlets and JSP: Login, Logout and a Shared Web Template
Four pages and one template: a login form that posts to a servlet, a session that remembers the user, a filter that guards home.jsp and account.jsp, and a logout that ends it, plus when each step forwards or redirects.
SQLite and a JDBC Connection Pool in a Java Web App
A whole database in one file, studyeasy.db, and a Java web app that talks to it: one HikariCP pool, a @WebListener that opens it and runs schema.sql at startup, and a /db-check servlet that borrows a connection.
Users CRUD With Servlets and JSP, Part 1: Add and List Through a DAO
Fill an empty users table from a form and list every row: UserController picks the page, UserDao holds all the SQL, SaveUserServlet inserts and redirects, and a JSTL table shows the rows. Plus why every value goes in as a ?.
