Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
HydraExpress Servlet Development Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

6.6 Implementing a Filter

A typical filter implementation consists of three main steps:

The Servlet Specification defines init() and destroy() methods for a filter. The filter should allocate resources in the init() method and release resources in the destroy() method. The servlet container calls the filter init() method before sending any requests through the filter, and calls the destroy() method before removing the filter from service.

The outline below shows a minimal filter implementation:

Line //1 dispatches request and response to the next filter in the filter chain. The chain forwards request and response to the doFilter() function of the next filter in the chain. Note that the chain->doFilter() call blocks until the doFilter() function on the next filter in the chain returns.

A filter must explicitly dispatch the request and response along the chain. This means that a filter can refuse to allow a request to reach a resource by returning without dispatching the request. If the filter does not dispatch the request along the chain, the response returns to the servlet container without reaching the original destination. For example, the code below shows a doFilter() function that limits access to a servlet:

In the code above, the filter does not forward the request along the chain if authorization fails but, instead, redirects the request to a servlet that handles unauthorized requests. The original servlet never receives the request.



Previous fileTop of DocumentContentsIndex pageNext file

© Copyright Rogue Wave Software, Inc. All Rights Reserved. All Rights Reserved. Rogue Wave is a registered trademark of Rogue Wave Software, Inc. in the United States and other countries. HydraExpress is a trademark of Rogue Wave Software, Inc. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.