What is a Filter?

 < Free Open Study > 



Like servlets, filters are web application components that can be bundled into a web application archive. However, unlike other web application components, filters are 'chained' to the container's processing pipeline. This means that they have access to an incoming request before the servlet processor does, and they have access to the outgoing response before it is returned to the client. This access allows filters to examine and modify the contents of both requests and responses.

start sidebar

Filters are web service components that add functionality to the request and response processing of a web application.

end sidebar

Due to their unique position in the request and response processing pipeline, filters can be a very useful addition to a developer's tool box. They can be used for:

  • Prototyping new functionality for a web application

  • Adding new functionality to legacy code

Filters are particularly useful in prototyping new concepts because their addition to the application occurs at deployment time. This means that filters can be added and removed from web applications without having to rewrite the underlying application code.

Prototyping New Functionality

Imagine a web site that wants to start charging for services it previously provided for free. To implement this we need to add billing information to certain web pages. In another attempt to increase revenue, we also want to add the site's best advertising banners to the pages that were most popular on the previous day.

This new functionality would be difficult to implement if the design of the original web application had not anticipated this scenario. However, adding this type of new functionality is made easy with filters. We could create two filters:

  • The first filter would examine the incoming request to determine if the requested resource is one of the pages we want to charge for. On completion of the standard page processing, the filter would append its own output (which details the billing information).

  • The second filter would maintain a list of the most frequently used pages for the previous day (probably obtained at startup from a database). The filter would examine the incoming requests to determine if the request is for one of the popular pages. If it is, the filter would generate an advertisement banner and append it to the pages by modifying the output stream sent to the client.

By inserting these two filters into the web application, we can quickly prototype our proposed implementation and any changes in the implementation can be made by simply modifying the code in the two filters - we don't have to touch the underlying web application.

Adding New Functionality

Imagine an application that is used by the accounting and sales departments of a company to obtain inventory information from a database. The same servlets, JSP pages, and static web pages are used by both departments to access the database information. However, due to recent restructuring of the company, it is now necessary to hide certain information from the sales department but to continue to allow the accounting department unrestricted access. How can we do this without having to rewrite the logic contained in the servlets and JSP pages?

A filter could be used to apply a 'patch' to the legacy code. The filter would determine from the incoming request if a user is a member of the sales department. After the underlying component has completed its processing, the filter can scan the output and remove any information that should not be displayed. By inserting this single filter into the processing pipeline, we can avoid having to modify the original application.



 < Free Open Study > 



Professional Java Servlets 2.3
Professional Java Servlets 2.3
ISBN: 186100561X
EAN: 2147483647
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net