Skip to content

Request Lifecycle

flowchart TD
    A[Client Request] --> B[Servlet Container<br>Tomcat/Undertow/Jetty]
    B --> C[Servlet Filters<br>Security, CORS, Logging]
    
    subgraph SpringBootApp [Spring Boot Application]
        C --> D[DispatcherServlet<br>Front Controller]
        D --> E{Handler Mapping}
        E --> F[Selected Controller]
        F --> G[Interceptors PreHandle]
        G --> H[Controller Method Execution]
        H --> I[Service Layer]
        I --> J[Repository Layer]
        J --> K[Database]
        K --> J
        J --> I
        I --> H
        H --> L[Interceptors PostHandle]
        L --> M[View Resolution]
        M --> N[Response Rendering]
        N --> O[Interceptors AfterCompletion]
    end
    
    O --> P[Servlet Filters<br>Response Processing]
    P --> Q[HTTP Response]
    Q --> R[Client]
    
    style SpringBootApp fill:dark,stroke:#01579b,stroke-width:2px
    style C fill:red,stroke:#ff6f00
    style P fill:red,stroke:#ff6f00
  • HTTP request arrives at the server
  • Request contains method, headers, parameters, and body
  • Request received by embedded servlet container (Tomcat, Undertow, or Jetty)
  • Container creates HttpServletRequest and HttpServletResponse objects

3. Servlet Filter Chain Execution (Pre-processing)

Section titled “3. Servlet Filter Chain Execution (Pre-processing)”
  • Security Filters: Authentication and authorization (Spring Security)
  • CORS Filters: Cross-origin request handling
  • Logging Filters: Request logging and monitoring
  • Character Encoding Filters: Request encoding setup
  • Each filter can process, modify, or reject the request
  • Front controller receives the filtered request
  • Central entry point for Spring MVC framework
  • Determines which controller method should handle the request
  • Based on URL patterns, HTTP methods, and other conditions
  • Custom pre-processing logic
  • Authentication checks
  • Request logging
  • Can short-circuit processing by returning false
  • Parameter binding (path variables, request params, request body)
  • Validation (if applicable)
  • Business logic invocation
  • Service layer method calls
  • Business logic implementation
  • Transaction management
  • Additional validation
  • Data access operations
  • Database interactions
  • ORM operations (if using JPA/Hibernate)
  • SQL query execution
  • Data retrieval or modification
  • Transaction management
  • Return from repository to service layer
  • Return from service to controller
  • Response object construction
  • Post-processing after controller execution
  • Response modification
  • Additional logging
  • View name to actual view resolution
  • Template processing (Thymeleaf, JSP, etc.)
  • Model data binding to view
  • Content generation (JSON, HTML, XML)
  • Response body creation
  • HTTP status and header setting

15. Handler Interceptors (AfterCompletion)

Section titled “15. Handler Interceptors (AfterCompletion)”
  • Request completion callbacks
  • Cleanup operations
  • Final logging

16. Servlet Filter Chain Execution (Post-processing)

Section titled “16. Servlet Filter Chain Execution (Post-processing)”
  • Response modification filters
  • Compression filters
  • Additional response headers
  • Response sent back through servlet container
  • Container manages connection and response writing
  • Client receives HTTP response
  • Browser or client application processes response
  • Execute before and after Spring MVC processing
  • Can intercept and modify requests and responses
  • Operate at the servlet container level
  • Central dispatcher for HTTP request handling
  • Coordinates all request processing components
  • Manages handler mapping, view resolution, etc.
  • Maps requests to handler methods
  • Supports various mapping strategies:
    • Annotation-based (@RequestMapping)
    • Controller class name patterns
    • Programmatic registration
  • Three interception points:
    • preHandle: Before controller execution
    • postHandle: After controller but before view rendering
    • afterCompletion: After complete request processing
  • @ControllerAdvice for global exception handling
  • @ExceptionHandler for controller-specific exceptions
  • HandlerExceptionResolver for custom exception resolution
  • ViewResolver strategies for different template engines
  • Content negotiation for different response types
  • Model data binding to views