17-01-2013, 02:38 PM
Servlets - Writing Filters
'
Servlets - Writing Filters.docx (Size: 25.27 KB / Downloads: 21)
Servlet Filters are Java classes that can be used in Servlet Programming for the following purposes:
• To intercept requests from a client before they access a resource at back end.
• To manipulate responses from server before they are sent back to the client.
There are are various types of filters suggested by the specifications:
• Authentication Filters.
• Data compression Filters
• Encryption Filters .
• Filters that trigger resource access events.
• Image Conversion Filters .
• Logging and Auditing Filters.
• MIME-TYPE Chain Filters.
• Tokenizing Filters .
• XSL/T Filters That Transform XML Content.
Filters are deployed in the deployment descriptor file web.xml and then map to either servlet names or URL patterns in your application's deployment descriptor.
When the web container starts up your web application, it creates an instance of each filter that you have declared in the deployment descriptor. The filters execute in the order that they are declared in the deployment descriptor.
Filters Application Order:
The order of filter-mapping elements in web.xml determines the order in which the web container applies the filter to the servlet. To reverse the order of the filter, you just need to reverse the filter-mapping elements in the web.xml file.
For example, above example would apply LogFilter first and then it would apply AuthenFilter to any servlet but the following example would reverse the order: