Skip to main content

Java Series #15: Spring MVC

Spring MVC Explained: Model–View–Controller, DispatcherServlet, and Why Multiple Dispatchers Exist

Published 3 min readMarkdown
Java Series #15: Spring MVC

MVC in One Breath

Spring MVC follows the Model–View–Controller pattern:

  • Model → Data + business logic result

  • View → UI (HTML via JSP, Thymeleaf, FreeMarker, etc.)

  • Controller → Handles requests and decides what to return

But controllers don’t receive requests directly.


DispatcherServlet: The Front Controller

The DispatcherServlet is the front controller of Spring MVC.

Every HTTP request enters the application through the DispatcherServlet.

Flow:

Client → DispatcherServlet → Controller → View → Client

So instead of requests going straight to controllers, DispatcherServlet sits in front and coordinates everything.

Why DispatcherServlet Exists

Before Spring MVC (Pure Servlet Era)

Earlier, every endpoint had its own servlet:

/login  → LoginServlet
/save   → SaveServlet
/delete → DeleteServlet

Why?

  • Tomcat only understands Servlets

  • Each URL had to be mapped to a servlet

Problems:

  • Too many servlets

  • Repeated boilerplate

  • HTML mixed inside Java

  • Hard to maintain

After Spring MVC

Now we have:

  • One servlet → DispatcherServlet

  • Many controllers → plain Java classes

/* → DispatcherServlet → @Controller methods

So yes, your understanding is correct:

Earlier, every endpoint needed its own servlet.
Now, a single DispatcherServlet receives all requests and maps them to the appropriate controller method.


DispatcherServlet → HandlerAdapter (What Happens Internally)

Once DispatcherServlet receives a request:

  1. It finds which controller method matches the URL

  2. It uses a HandlerAdapter to invoke that method

  3. It processes the return value (Model, View, JSON, etc.)

  4. It resolves the view and sends the response

You don’t call HandlerAdapter directly — Spring handles it internally.

Controllers Are NOT Servlets

This is a key distinction.

  • Controllers do not extend HttpServlet

  • They are just annotated Java classes:

      @Controller
      @GetMapping("/save")
    

The only servlet involved is DispatcherServlet.

View Layer in Spring MVC

The View is responsible for rendering UI.

Common view technologies:

  • JSP

  • Thymeleaf

  • FreeMarker

DispatcherServlet doesn’t render views itself — it delegates that job to a ViewResolver, based on configuration.

WebApplicationContext per DispatcherServlet

Important architectural detail:

Each DispatcherServlet has its own WebApplicationContext

This means:

  • Separate controllers

  • Separate view resolvers

  • Separate interceptors

  • Separate MVC configuration

They may share a parent (root) context for services and repositories.


When Do We Need Multiple DispatcherServlets?

Most applications need only one.

Multiple DispatcherServlets are used only when you want strict isolation between web modules.

1. Different URL Spaces, Different Responsibilities

/admin/* → AdminDispatcherServlet
/api/*   → ApiDispatcherServlet

Why?

  • Admin UI uses JSP or Thymeleaf

  • API returns JSON only

  • Separate configs stay clean

2. Different Security Requirements

/admin/*  → heavy authentication, RBAC
/public/* → open endpoints

Each dispatcher can have:

  • Different interceptors

  • Different security rules

3. Multiple Products in One Deployment

/productA/*
/productB/*

Each product:

  • Its own controllers

  • Its own views

  • Its own web configuration

4. Legacy + Modern Codebase

Common in enterprises:

  • Old XML-based MVC module

  • New annotation-based module

Instead of rewriting everything, teams isolate them using multiple DispatcherServlets.

Different View Technologies per Dispatcher

Example:

/web/* → JSP / Thymeleaf
/api/* → JSON (no views)
/admin/* → FreeMarker

Each DispatcherServlet:

  • Has its own ViewResolver

  • Avoids configuration conflicts

What Multiple Dispatchers Are NOT For

Performance, Scalability, Load balancing, Microservices

If you’re thinking microservices → separate applications, not multiple dispatchers.

Spring Boot Reality

In Spring Boot:

  • DispatcherServlet is auto-configured

  • One dispatcher is the default (and recommended)

  • Multiple dispatchers are rare and advanced


Final Mental Model

  • Servlet → low-level HTTP handler (Tomcat understands this)

  • DispatcherServlet → Spring’s smart servlet

  • Controller → request handler logic

  • View → UI rendering (JSP, Thymeleaf, FreeMarker)

  • Multiple DispatcherServlets → used only for strong modular isolation

SpringMVc

About the author

  • I’m a Full-stack Developer who enjoys turning ideas into simple and useful experiences. I like building clean user interfaces, exploring AI, and understanding how things work behind the scenes. From creating websites to trying out new technologies, I’m always curious and learning something new. Next.js, Node.js, MongoDB, and Java Spring Boot are the tools I use regularly. I enjoy experimenting with projects, sharing what I learn, and improving with every build. Vibing + Thinking.

No comments yet. Be the first to comment.