Wednesday, November 25, 2015

Servlet Filters

A filter is a small web components that dynamically intercepts requests and responses to view and manipulate data that is exchanging between client and server. Filter will intercept and examine the request before the servlet invocation. This will modify the request by changing header and data of a request by creating a wrapper for the real request (I/P filtering). Also they will intercept and modify the response after the servlet invocation by creating a wrapper for the real response (O/P filtering) .

They are preprocessors of the request before it reaches a servlet, and postprocessors of the response leaving a servlet.

Servlet filters are pluggable Web components. We are defining this in web.xml. We can add and remove the entry from the web.xml without changing the servlet.
There can be more than one filter to one or more servlets.2 filters can have same url mapping
It will execute in the order that you placed in web.xml to execute.

Servlet filters also can attach to JavaServer Pages (JSP) files and HTML pages.

When to Use Filters:

Use a Filter when you want to filter and/or modify requests based on specific conditions.
Logging data about the request or response, processing security protocols, managing session attributes, and more.
  •  User authentication/security: If additional authentication or additional processing is required before and after authentication. Placing security logic into a filter rather than a servlet or JSP page provides tremendous flexibility. During development, the filter can be turned off (comment out of web.xml). In production, the filter is turned back on. Also, multiple filters can be added to provide increasing levels of security, encryption, and non-repudiation services as necessary.
  • Logging: Logging (as browser type, time of day, forwarding URL, etc ) data about the request or response.
  • Performance: decompress the request and compress the response.
  • Session-handling: Using a filter to manage your session data .
  • XSLT transformation: The filter will intercept this XML before it is sent to the client browser and apply an XSLT transformation.

No comments:

Post a Comment