Introduction To Servlet Filter and Handler Interceptor

What is the difference between Servlet Filter & Handler Interceptor ?

Vinesh
The Jabberjays

--

What is a Servlet Filter?

A filter performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, a reference to the ServletContext which it can use.

The servlet container calls the init method exactly once after instantiating the filter. The init method must complete successfully before the filter is asked to do any filtering work.

Filter performs the filtering operation in the doFilter method, and it is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.

The destroy method is only called once all threads within the filter’s doFilter method have exited or after a timeout period has passed. This method gives the filter an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the filter’s current state in memory.

--

--

Vinesh
The Jabberjays

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