Filters

Filter is a Java class that sits between a Web server and a servlet. A filter intercepts HTTP requests sent to a servlet and can transform the requests sent to and the responses received from the servlet.

Filters are a new feature introduced in the Java Servlet API 2.3 specification. Since WebLogic Server 7.0 supports this specification, it provides support for developing and deploying filters in the WebLogic Server environment.

Concepts

You can utilize filters effectively in these areas:

  • Encryption

  • Compression

  • Content transformation

  • Debugging and tracing

  • Logging and audit trails

The Filter Java class implements the Filter interface. The Filter interface contains the following methods:

  • doFilter() This method must be implemented by the Filter class. All the functionality that the filter performs resides in this method. The doFilter() method has three parameters: ServletRequest, ServletResponse, and FilterChain. The input received from the browser is wrapped in the ServletRequest class. Any output can be generated by the filter using the ServletResponse class. A filter can modify the request and response as required. The FilterChain class is used to invoke the next filter in the chain of filters or the final servlet.

  • getFilterConfig() Filters have a FilterConfig class, similar to the ServletConfig class. To obtain a reference to the FilterConfig object, you should use the getFilterConfig() method.

  • setFilterConfig() Use the setFilterConfig() method to set a FilterConfig object for the filter. The method takes a FilterConfig object as a parameter.

The Filter class must implement these methods and provide the actual filter functionality in the doFilter() method.

Filter Life Cycle

Filters have a life cycle similar to the servlet cycle. The life cycle involves these methods:

  • init() The init() method is used to initialize the filter. This method is called only once in the life cycle of the filter.

  • doFilter() The doFilter() method is called throughout the life cycle of the filter after the filter is initialized.

  • destroy() The destroy() method is called when the filter is shut down by the WebLogic Server. The destroy() method can be used to free up any resources opened by the filter.

Notice the similarity between filters and servlets.

Filter Chaining

Since filters are preprocessors of an HTTP request and response, you can have more than one filter for a servlet. For example, if you have written two filters for a servlet, one that performs encryption and another that performs logging, you can have the logging filter preprocess the request and response and call the encryption filter to enable it to perform its preprocessing. This is called filter chaining since the logging and encryption filters are part of a chain of filters for a servlet. Filters for a servlet can be chained by defining the list of filters and their mappings to servlets or URLs in the deployment descriptor file web.xml. The WebLogic Server determines the list of filters mapped to a URL or a servlet by matching the filters defined in the filter-mapping tag. The filters are chained in the order in which they are defined in the deployment descriptor file.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

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