Spring Boot : Difference between @WebFilter vs @Component

There are two ways to use filter in Spring boot (assume that you have only one filter):

This page will explain about is the difference between those two ways.

First difference: @WebFilter can add url pattern, but @Component cannot .

Second difference: if you download and run the example in Spring Boot with all examples  and go to this url:


This request is to call the mapping and forward to JSP by Spring Boot.

You will get these lines in log:

... : ServletFilter:http://localhost:8080/test/testjsp
... : SpringBootFilter:http://localhost:8080/test/testjsp
... : SpringBootFilter:http://localhost:8080/test/WEB-INF/jsp/testjsp-view.jsp

Line 1 is from Servlet Filter (or from @WebFilter )

Line 2 and 3 are from SpringBoot Filter ( or from @Component )

You can see that the request passed the  Servlet Filter only once, which is the default behavior as Servlet filter mapping as REQUEST (detail in here )

While the same request passed Spring Boot Filter twice (line 2 and 3) one for the controller and another one for the jsp, which is similar to Servlet filter mapping as FORWARD (detail in the same link )

I personally prefer the Servlet Filter way, because it is acting like the standard Servlet Filter. One request should pass filter only once even it will later forward to JSP or not.