- Filters are used to pre process the request and post process the response.
- This concept has introduced in servlet 2.3 version.
- Preprocessing involves authentication , logging ,authorization , change request information.
- Post processing involves encrypting response compressing response.
Application areas of Filter:
- To implement logging mechanism.
- To perform authentication.
- To perform authorization.
- To alter request information.
- To alter response information.
- Encrypting response.
- Compressing response.
Interfaces present in Filter API:
- Javax.servlet.Filter
- Javax.servlet.FilterConfig
- Javax.servlet.FilterChain
Filter:
- Every filter class should implement filter interface either directly or indirectly.
- Filter interface defines the most common which can be applicable for any Filter object.
- Filter interface defines following methods
- init()
- doFilter()
- destroy()
- public abstract void init(FilterConfig config) Throws ServletException:
This method is used to perform initialization activities.
This method will be executed just after filter object is created. - public abstract void doFiletr(ServletRequest req, ServletResponse res, FilterChain ch) throws
IOException, ServletException.
This method will be executed for every request to perform filter activities.
In this method only we have to implement entire Filter logic
This method takes filterChain object as argument and by using that we can forward the request
to the nest level. Next level can be again filter or servlet. - public abstract void destrroy();
this method will be executed only once to perform cleanup activities just before taking filter from out of service .
You might like:
No comments