A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses. Filters typically do not themselves create responses, but instead provide universal functions that can be "attached" to any type of servlet or JSP page.
In Other words , A filter is an object that is used to perform filtering tasks such as conversion, log maintain, compression, encryption and decryption, input validation etc.
A filter is invoked at the preprocessing and postprocessing of a request. It 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 it will be easier to maintain the web application.
Usage of Filter:-
Filters are used for different functions as below:-
1-Authentication-Blocking- requests based on user identity.
2-Logging and auditing-Tracking users of a web application
3- Image conversion-Scaling maps, and so on.
4- Data compression-Making downloads smaller.
5- Localization-Targeting the request and response to a particular locale
6- XSL/T transformations of XML content-Targeting web application responses to more that one type of client.
7- to encapsulate recurring tasks in reusable units
8- encryption and decryption
9- mime-type chaining, and caching
Difference Between Servlet And Filter:-
| Filter | Servlet |
|---|---|
1-Filter is used for filtering the request and perform some action like authenticity of session, user is valid or not for that request, etc. | 1-Servlet is used for performing the action which needs to be taken for particular request like user login, get the response based on user role, interacts with database for getting the data, business logic execution etc |
2-Filters differ from web components in that filters usually do not themselves create a response. | 2-Servlet technology is used to create web application where it creates response of client’s request themselves. |
3- For authorization, a Filter is the best suited This is because they can be configured to run for all pages of a site. So you only need one filter to protect all your pages. | 3-Not as much as comparison to Filter. |





0 comments:
Post a Comment