A filter is an object that is invoked at the preprocessing and postprocessing of a request.
It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation etc.
The servlet filter is pluggable, i.e. its entry is defined in the web.xml file, if we remove the entry of filter from the web.xml file, filter will be removed automatically and we don't need to change the servlet.
So maintenance cost will be less.
Note: Unlike Servlet, One filter doesn't have dependency on another filter.
Usage of Filter
- recording all incoming requests
- logs the IP addresses of the computers from which the requests originate
- data compression
- encryption and decryption
- input validation etc.
Advantage of Filter
- Filter is pluggable.
- One filter don't have dependency onto another resource.
- Less Maintenance
Like servlet filter have its own API. The javax.servlet package contains the three interfaces of Filter API.
1) Filter interface
For creating any filter, you must implement the Filter interface. Filter interface provides the life cycle methods for a filter.
|public void init(FilterConfig config)||init() method is invoked only once. It is used to initialize the filter.|
|public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain)||doFilter() method is invoked every time when user request to any resource, to which the filter is mapped.It is used to perform filtering tasks.|
|public void destroy()||This is invoked only once when filter is taken out of the service.|
2) FilterChain interface
The object of FilterChain is responsible to invoke the next filter or resource in the chain.This object is passed in the doFilter method of Filter interface.The FilterChain interface contains only one method:
- public void doFilter(HttpServletRequest request, HttpServletResponse response): it passes the control to the next filter or resource.
How to define Filter
We can define filter same as servlet. Let's see the elements of filter and filter-mapping.
For mapping filter we can use, either url-pattern or servlet-name. The url-pattern elements has an advantage over servlet-name element i.e. it can be applied on servlet, JSP or HTML.
Simple Example of Filter
In this example, we are simply displaying information that filter is invoked automatically after the post processing of the request.
|For defining the filter, filter element of web-app must be defined just like servlet.|
download this example (developed using Myeclipse IDE)
download this example (developed using Eclipse IDE)
download this example (developed using Netbeans IDE)
Next TopicAuthentication Filter
- Why do I hate engineering
- How is like with a geologist degree
- What makes you love playing online
- Why are Americans Jews less religious nowadays
- Whats this a meant for in HTML
- Why is deception considered wrong
- Is 20 days menstrual cycle normal
- Arabic Sweets how do I cook Halawa
- Why dont dead bodies bleed
- Is hyphae a part of bryophyllum
- What are 4 dimentional Objects
- Could you gain consciousness after you die
- Why is Drone Racing considered a sport
- Are we humans a primitive species
- What relation has academic achievements with success
- What are the basics of influencer marketing
- Where are the NYC MTA trains made
- How fast does paint dry