SteelReed

Search

Search posts, questions…

OOP & DesignQuestion 27 / 42

Middle

What is Chain of Responsibility? How do Servlet Filters and the Spring Security filter chain use it?

Short answer

Chain of Responsibility: a request goes through a chain of handlers, and each handler either handles it or passes it to the next. The sender doesn't know who will handle it; adding a step = adding a handler to the chain, without touching the others.

chain.doFilterbad token: return 401,don't pass onchain.doFilterHTTP requestLoggingFilterAuth filterChain stopsDispatcherServletController

3 points to say:

  1. Each handler handles or passes on; stopping the chain is a deliberate decision (401, 429).
  2. Real examples: the Servlet Filter chain, Spring Security SecurityFilterChain, HandlerInterceptor, validation / discount rule chains.
  3. Traps: order matters, and forgetting chain.doFilter silently swallows the request.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.