12 · Spring Boot and the web request lifecycle / LESSON 54 OF 102
Servlets, DispatcherServlet, filters, and interceptors
An HTTP request crosses several boundaries before a controller method runs.
Before this lesson: Profiles, property precedence, and secrets
Understand the topic
Servlet filters wrap requests before the DispatcherServlet, which finds a handler, binds arguments, invokes it, and converts the response. Spring MVC interceptors participate around mapped handlers. Security operates primarily in the filter chain, so a controller-only test does not prove security behavior.
Use a filter for transport concerns shared across servlet traffic, and an interceptor for handler-related concerns. Keep business rules in services. A request may be rejected before controller execution due to authentication, binding, or validation.
EXAMPLE · JAVA
RequestIdFilter.java
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
public class RequestIdFilter extends OncePerRequestFilter {
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain chain) throws ServletException, IOException {
response.setHeader("X-Request-Id", java.util.UUID.randomUUID().toString());
chain.doFilter(request, response);
}
}What to expect
Register this filter once as a bean or through a FilterRegistrationBean. Responses receive a server-generated request ID.
YOUR TURN
Put it into practice
- 01Trace one request through a filter, controller, and service.
- 02Trigger a binding failure and compare which stages still execute.
- 03Verify: You identify the layer producing each response status without assuming every request reaches the controller.
Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.
UNDER THE HOOD
Build the mental model
An HTTP request crosses several boundaries before a controller method runs.
- 01Servlet filters wrap requests before the DispatcherServlet, which finds a handler, binds arguments, invokes it, and converts the response. Spring MVC interceptors participate around mapped handlers. Security operates primarily in the filter chain, so a controller-only test does not prove security behavior.
- 02Use a filter for transport concerns shared across servlet traffic, and an interceptor for handler-related concerns. Keep business rules in services. A request may be rejected before controller execution due to authentication, binding, or validation.
- 03Trace the example: Register this filter once as a bean or through a FilterRegistrationBean. Responses receive a server-generated request ID.
A mistake worth avoiding
A request returns 401 without hitting the controller breakpoint.
DEBUGGING ROOM
Something broke. Where do you look?
A request returns 401 without hitting the controller breakpoint.
Before revealing the diagnosis, write down your first investigation step.
CHECK YOUR UNDERSTANDING
Can a filter reject a request before its controller runs?
BUILD IT YOURSELF
Trace one request through a filter, controller, and service.
- 1.Trace one request through a filter, controller, and service.
- 2.Trigger a binding failure and compare which stages still execute.
- 3.Reproduce this failure, then repair it: A request returns 401 without hitting the controller breakpoint.
Acceptance checklist
Lab completion is your self-report. Knowledge checks do not certify project quality.
RETRIEVE IT FROM MEMORY
Can a filter reject a request before its controller runs?
Revisit this lesson in three days after a successful check or lab.
Ready for the next step?
You identify the layer producing each response status without assuming every request reaches the controller.
Reading is separate from passing a knowledge check or completing a lab.