Skip to content

Commit 2b15ad6

Browse files
committed
Document custom servlet filters in Web MVC section
Add information to the Filters section about creating custom filters by implementing `javax.servlet.Filter` or extending Spring's `GenericFilterBean` and `OncePerRequestFilter`. This brings consistency with the WebFlux documentation, which already explains custom filters via `WebFilter`. Signed-off-by: 99hyeon <[email protected]>
1 parent dd8313f commit 2b15ad6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

framework-docs/modules/ROOT/pages/web/webmvc/filters.adoc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,35 @@ Servlet filters can be configured in the `web.xml` configuration file or using S
1515
If you are using Spring Boot, you can
1616
{spring-boot-docs}/how-to/webserver.html#howto.webserver.add-servlet-filter-listener.spring-bean[declare them as beans and configure them as part of your application].
1717

18+
In addition to the built-in filters, you can also create custom filters by implementing the standard
19+
`javax.servlet.Filter` interface or extending one of Spring's convenient base classes:
20+
21+
* `GenericFilterBean` – Useful when you need access to Spring-managed beans or the application context.
22+
* `OncePerRequestFilter` – Ensures a single execution per request, even for asynchronous or error dispatches.
23+
24+
These base classes simplify filter development and offer integration with Spring's `ApplicationContext`.
25+
26+
For example:
27+
28+
[source,java]
29+
----
30+
public class MyFilter extends OncePerRequestFilter {
31+
32+
@Override
33+
protected void doFilterInternal(HttpServletRequest request,
34+
HttpServletResponse response,
35+
FilterChain filterChain)
36+
throws ServletException, IOException {
37+
// Pre-processing logic
38+
System.out.println("Processing request: " + request.getRequestURI());
39+
40+
filterChain.doFilter(request, response);
41+
42+
// Post-processing logic
43+
}
44+
}
45+
----
46+
1847

1948
[[filters-http-put]]
2049
== Form Data

0 commit comments

Comments
 (0)