Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

JSP and Servlets
  • Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
  • Set Up Tomcat 11 and a Maven Web Project in IntelliJ IDEA
  • The Servlet Request Lifecycle: @WebServlet, web.xml, init, doGet and destroy
  • Servlet Request Parameters, and Forward vs Redirect
  • Run a Java Web App in Docker: Tomcat 11 With One Compose Command
  • Sessions, Cookies and Filters in Java Servlets: Log In Once, Stay Logged In
  • Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
  • JSP Scripting: Expressions, Scriptlets, Declarations, Comments and Includes
  • JavaBeans and JSP Scopes: Page, Request, Session and Application
  • JSP Expression Language Step by Step: ${...} for Beans, Maps, Lists, Params and Scopes
  • JSTL Core Tags: c:set, c:out, c:if, c:choose and c:forEach
  • JSTL Functions and Formatting: fn:length, fn:toUpperCase, fn:contains, fmt:formatNumber and fmt:formatDate
  • Maintaining and Migrating Legacy JSP With AI: Explain the Page, Refactor to a DAO, Servlet and JSTL, Then Move It to React
  • MVC and the Front Controller With Servlets and JSP
  • Forms With JSP and Servlets: Handle the POST and Validate on the Server
  • Mini App With Servlets and JSP: Login, Logout and a Shared Web Template
  • SQLite and a JDBC Connection Pool in a Java Web App
  • Users CRUD With Servlets and JSP, Part 1: Add and List Through a DAO
  • Users CRUD With Servlets and JSP, Part 2: Update and Delete
  1. Blog
  2. /
  3. JSP and Servlets
  4. /
  5. MVC

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026MVC

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. /books calls listBooks, /home calls home.
  • Anything else, such as /app/orders, gets sendError(404). So does /app on 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.

The browser sends /app/books, /app/home or /app/orders to one FrontController on /app/*. It reads getPathInfo. /books goes to listBooks, which puts three books on the request and returns books, forwarded to books.jsp. /home goes to home, which puts count 3 and returns home, forwarded to home.jsp. /orders has no handler and gets 404. Browser/app/... FrontController /app/* getPathInfo() listBooksbooks = 3 books homecount = 3 /orders404, no handler books.jsp home.jsp
One servlet takes every /app request and picks the handler. Handlers fill the model; views under WEB-INF draw it.

One servlet per URL or one in front?

Servlet per URLFront controller
Classesone per pageone class on /app/*
Add a pagea new servlet classa new handler and a view
Shared steps (a login check)copied into each servletwritten once
Where you meet itsmall appsinside 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: /books fails with HTTP 500, "The method getTitle() from the type Book is not visible".
  • Mapping @WebServlet("/app") without /*. It only answers /app itself, and /app/books is 404.

Quick recap

PartHereJob
ModelBookholds data, knows nothing about the web
ControllerBookController, FrontControllertakes the request, gets the model, picks the view
ViewWEB-INF/views/*.jspturns 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.

Keep going in JSP and Servlets · 14 of 19

Previous
Maintaining and Migrating Legacy JSP With AI: Explain the Page, Refactor to a DAO, Servlet and JSTL, Then Move It to React
Next post
Forms With JSP and Servlets: Handle the POST and Validate on the Server
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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 ?.

On this page

  • The model is plain data
  • The controller decides
  • The view, behind WEB-INF
  • A front controller
  • One servlet per URL or one in front?
  • Common mistakes
  • Quick recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.