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. ConceptsYou can utilize filters effectively in these areas:
The Filter Java class implements the Filter interface. The Filter interface contains the following methods:
The Filter class must implement these methods and provide the actual filter functionality in the doFilter() method. Filter Life CycleFilters have a life cycle similar to the servlet cycle. The life cycle involves these methods:
Notice the similarity between filters and servlets. Filter ChainingSince 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. |