Servlet Filter and Handler Interceptor- Spring boot Implementation

Access ServletRequest multiple times, sequence of execution

Vinesh
The Jabberjays

--

Filters and interceptors are an important part of rest applications, I have already briefed about both of them and gave a comparison between the 2 in my previous article, please check it out.

How to implement Filters?

The Servlet Filter implementation is really simple, we implement the javax.servlet.Filter, the @Order specifies the order in which the filters have to be executed. We have specified the order as 1 so this will be the first filter to be executed. The chain.doFilter called in line 18 passes the request and response object to the next filter in chain or to the handler if that’s the last filter.

Inside the doFilter method, I have checked if I can access the user IP address, requestparams from request, payload i.e., the request body from the request object. But the problem we face is the request can be read only once and if we try to access it in filter, it’ll give an…

--

--

Vinesh
The Jabberjays

Coder 👨‍💻, Gamer🎮 and a dog person🐶. That says a lot about me, I guess😛.