Filter’s life cycle


Every filter must implement the three methods in the Filter interface:
init()
doFilter()
destroy()


init()
When the Container decides to instantiate a filter, the init() method, do the set-up tasks before the filter is called.

doFilter()
This method does the heavy lifting.
The doFilter() method is called every time the Container determines that the filter should be applied to the current request.
The doFilter() method takes three arguments:
o A ServletRequest (not an HttpServletRequest)!
o A ServletResponse (not an HttpServletResponse)!
o A FilterChain

The doFilter() method is used to implement your filter’s function. If your filter is supposed to log user name to a file, do it in doFilter().

destroy()
When the Container decides to remove a filter instance, it calls the destroy() method, giving you a chance to do any cleanup you need to do before the instance is destroyed.

The basic flow of filter based application:


-secondry text-uppercase" style="font-weight:600;"> Contact us