Spring MVC DispatcherServlet Interview Questions and Answers

Master Spring MVC DispatcherServlet with interview questions covering Front Controller, request lifecycle, HandlerMapping, HandlerAdapter, ViewResolver, HandlerInterceptor, HttpMessageConverter, exception handling, and production best practices.


Introduction

Every HTTP request in a Spring MVC application passes through a single central component called the DispatcherServlet.

It acts as the Front Controller, coordinating the complete request lifecycle.

Instead of clients communicating directly with controllers, every request first reaches the DispatcherServlet, which decides

  • Which controller should execute
  • Which method should be called
  • Which interceptors should run
  • How exceptions should be handled
  • How responses should be generated

Understanding the DispatcherServlet is one of the most frequently asked Spring MVC interview topics.


DispatcherServlet Architecture

flowchart LR

Client --> DispatcherServlet

DispatcherServlet --> HandlerMapping

HandlerMapping --> Controller

Controller --> Service

Service --> Repository

Repository --> Database

Controller --> ViewResolver

ViewResolver --> HTMLorJSON

HTMLorJSON --> Client

Q1. What is DispatcherServlet?

Answer

DispatcherServlet is the Front Controller of Spring MVC.

It is responsible for coordinating every web request.

Responsibilities

  • Receive HTTP requests
  • Find the correct controller
  • Execute controller methods
  • Handle exceptions
  • Resolve views
  • Return HTTP responses

Every Spring MVC application contains one DispatcherServlet.


Q2. Why is DispatcherServlet called the Front Controller?

Instead of multiple controllers handling requests independently,

all requests first arrive at DispatcherServlet.

Front Controller Pattern

flowchart LR

Browser --> DispatcherServlet

DispatcherServlet --> Controller1

DispatcherServlet --> Controller2

DispatcherServlet --> Controller3

Benefits

  • Centralized request handling
  • Security integration
  • Logging
  • Exception handling
  • Interceptors
  • Consistent request processing

Q3. What is the request lifecycle in DispatcherServlet?

Workflow

  1. Request arrives.
  2. DispatcherServlet receives it.
  3. HandlerMapping finds controller.
  4. HandlerAdapter invokes controller.
  5. Business logic executes.
  6. Model returned.
  7. ViewResolver resolves view.
  8. Response sent back.

Request Lifecycle

sequenceDiagram
Browser->>DispatcherServlet: HTTP Request
DispatcherServlet->>HandlerMapping: Find Handler
HandlerMapping-->>DispatcherServlet: Controller
DispatcherServlet->>HandlerAdapter: Execute
HandlerAdapter->>Controller: Invoke Method
Controller->>Service: Business Logic
Service-->>Controller: Result
Controller-->>DispatcherServlet: Model/View
DispatcherServlet->>ViewResolver: Resolve
ViewResolver-->>DispatcherServlet: View
DispatcherServlet-->>Browser: Response

Q4. What is HandlerMapping?

HandlerMapping determines which controller should process a request.

Example

@GetMapping("/customers")

Incoming request

GET /customers

HandlerMapping locates the appropriate controller method.


Q5. What is HandlerAdapter?

Controllers can have different method signatures.

HandlerAdapter provides a common way to invoke them.

Responsibilities

  • Invoke controller
  • Resolve method arguments
  • Handle return values

HandlerAdapter

flowchart LR

DispatcherServlet --> HandlerAdapter

HandlerAdapter --> ControllerMethod

Developers rarely implement HandlerAdapter directly.


Q6. What is ViewResolver?

After controller execution,

DispatcherServlet asks ViewResolver which view should render the response.

Supported views

  • Thymeleaf
  • JSP
  • Mustache
  • FreeMarker

Example

return "dashboard";

ViewResolver converts

dashboard

↓

dashboard.html

Q7. How are REST responses returned?

REST controllers usually return JSON.

DispatcherServlet uses HttpMessageConverters.

Example

@RestController

@GetMapping("/users")

REST Flow

flowchart LR

Controller --> HttpMessageConverter

HttpMessageConverter --> JSON

JSON --> Browser

No ViewResolver is involved.


Q8. How does DispatcherServlet handle exceptions?

If an exception occurs

Controller

↓

Exception

↓

DispatcherServlet

↓

ExceptionResolver

↓

HTTP Response

Spring supports

  • @ExceptionHandler
  • @ControllerAdvice
  • HandlerExceptionResolver

This centralizes exception handling.


Q9. What are Interceptors?

Interceptors execute before and after controller execution.

Common uses

  • Authentication
  • Logging
  • Auditing
  • Performance monitoring

Interceptor Flow

flowchart LR

Request --> PreHandle

PreHandle --> Controller

Controller --> PostHandle

PostHandle --> AfterCompletion

AfterCompletion --> Response

Interceptors work closely with DispatcherServlet.


Q10. DispatcherServlet Best Practices

Keep Controllers Thin

Delegate business logic.


Use Global Exception Handling

Avoid repetitive try-catch blocks.


Validate Input

Use Bean Validation.


Use Interceptors

Implement logging and auditing centrally.


Prefer REST APIs

Use @RestController for modern applications.


Banking Example

flowchart TD

MobileApp --> DispatcherServlet

DispatcherServlet --> AuthInterceptor

AuthInterceptor --> PaymentController

PaymentController --> PaymentService

PaymentService --> Database

PaymentService --> JSON

JSON --> MobileApp

The DispatcherServlet orchestrates authentication, controller execution, and response generation.


Common Interview Questions

  • What is DispatcherServlet?
  • Why is it called Front Controller?
  • Explain DispatcherServlet lifecycle.
  • What is HandlerMapping?
  • What is HandlerAdapter?
  • What is ViewResolver?
  • What is HttpMessageConverter?
  • How are exceptions handled?
  • What are Interceptors?
  • DispatcherServlet best practices?

Quick Revision

Topic Summary
DispatcherServlet Front Controller
HandlerMapping Finds controller
HandlerAdapter Invokes controller
Controller Handles request
Service Business logic
ViewResolver Resolves views
HttpMessageConverter Converts Java ↔ JSON/XML
ExceptionResolver Handles exceptions
Interceptor Pre/Post request processing
Response HTML or JSON

Complete DispatcherServlet Lifecycle

sequenceDiagram
Browser->>DispatcherServlet: HTTP Request
DispatcherServlet->>HandlerMapping: Locate Controller
HandlerMapping-->>DispatcherServlet: Handler
DispatcherServlet->>Interceptor: preHandle()
Interceptor-->>DispatcherServlet: Continue
DispatcherServlet->>HandlerAdapter: Execute
HandlerAdapter->>Controller: Invoke
Controller->>Service: Business Logic
Service-->>Controller: Result
Controller-->>HandlerAdapter: Return Model
HandlerAdapter-->>DispatcherServlet: Model/View
DispatcherServlet->>ViewResolver: Resolve
ViewResolver-->>DispatcherServlet: View
DispatcherServlet->>Interceptor: postHandle()
DispatcherServlet-->>Browser: HTTP Response

Internal DispatcherServlet Components

flowchart TD

DispatcherServlet --> HandlerMapping

DispatcherServlet --> HandlerAdapter

DispatcherServlet --> HandlerInterceptor

DispatcherServlet --> Controller

DispatcherServlet --> ExceptionResolver

DispatcherServlet --> ViewResolver

DispatcherServlet --> HttpMessageConverter

HttpMessageConverter --> JSON

ViewResolver --> HTML

Each component has a specialized responsibility, making Spring MVC highly extensible.


Production Example – Banking Fund Transfer API

A banking application exposes

POST /payments

Workflow

  1. Customer submits a payment request.

  2. DispatcherServlet receives the request.

  3. Authentication interceptor validates the JWT.

  4. HandlerMapping locates PaymentController.

  5. HandlerAdapter invokes the controller.

  6. PaymentService performs:

    • Validation
    • Fraud checks
    • Database transaction
  7. Controller returns a PaymentResponse.

  8. HttpMessageConverter serializes the object into JSON.

  9. DispatcherServlet sends the response.

@RestController
@RequestMapping("/payments")
public class PaymentController {

    @PostMapping
    public PaymentResponse transfer(
            @RequestBody PaymentRequest request){

        return paymentService.transfer(request);

    }

}
flowchart LR

MobileApp --> DispatcherServlet

DispatcherServlet --> JWTInterceptor

JWTInterceptor --> HandlerMapping

HandlerMapping --> PaymentController

PaymentController --> PaymentService

PaymentService --> PostgreSQL

PaymentService --> PaymentResponse

PaymentResponse --> HttpMessageConverter

HttpMessageConverter --> JSON

JSON --> MobileApp

Production Benefits

  • Centralized request processing.
  • Authentication before controller execution.
  • Consistent exception handling.
  • Automatic JSON serialization.
  • Easy monitoring through interceptors.
  • Clean separation of web and business layers.

DispatcherServlet vs Front Controller Pattern

Feature DispatcherServlet Generic Front Controller
Framework Spring MVC Design Pattern
Request Routing Automatic Manual
Handler Mapping Built-in Custom Implementation
View Resolution Built-in Custom
Exception Handling Built-in Manual
REST Support HttpMessageConverter Custom
Interceptors Built-in Custom
Extensibility Very High Depends on implementation

Key Takeaways

  • DispatcherServlet is the central Front Controller of Spring MVC and coordinates the complete HTTP request lifecycle.
  • It collaborates with HandlerMapping, HandlerAdapter, ViewResolver, ExceptionResolver, Interceptors, and HttpMessageConverters.
  • HandlerMapping selects the appropriate controller, while HandlerAdapter invokes it regardless of method signature.
  • ViewResolver is used for MVC applications, whereas HttpMessageConverters generate JSON or XML responses for REST APIs.
  • Interceptors provide centralized request processing for authentication, logging, auditing, and performance monitoring.
  • Global exception handling through @ControllerAdvice keeps controllers clean and ensures consistent error responses.
  • Understanding the DispatcherServlet lifecycle is fundamental for debugging, extending, and optimizing Spring MVC applications.
  • DispatcherServlet is one of the most important and frequently discussed components in Spring MVC interviews and enterprise application architecture.