14-2 Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server
bottom of the chain. This process continues in reverse order up through the chain of filters.
14.1.2 Uses for Filters
Filters can be useful for the following functions:
■
Implementing a logging function
■
Implementing user-written security functionality
■
Debugging
■
Encryption
■
Data compression
■
Modifying the response sent to the client. However, post processing the response can degrade the performance of your application.
14.2 Writing a Filter Class
To write a filter class, implement the javax.servlet.Filter interface see http:java.sun.comj2eetutorialapijavaxservletFilter.html
. You must implement the following methods of this interface:
■
init
■
destroy
■
doFilter You use the doFilter method to examine and modify the request and response
objects, perform other tasks such as logging, invoke the next filter in the chain, or block further processing.
Several other methods are available on the FilterConfig object for accessing the name of the filter, the ServletContext and the filters initialization attributes. For
more information see the J2EE Javadocs for javax.servlet.FilterConfig at http:java.sun.comj2eetutorialapiindex.html
. To access the next item in the chain either another filter or the original resource, if that
is the next item in the chain, call the FilterChain.doFilter method.
14.3 Configuring Filters
You configure filters as part of a Web application, using the applications web.xml deployment descriptor. In the deployment descriptor, you specify the filter and then
map the filter to a URL pattern or to a specific servlet in the Web application. You can specify any number of filters.
14.3.1 Configuring a Filter
To configure a filter:
1.
Open the web.xml deployment descriptor in a text editor or use the Administration Console. For more information, see
Section 2.4, Web Application
Note: The filter can modify the headers only if the response has not
already been committed.
Filters 14-3
Developer Tools . The web.xml file is located in the WEB-INF directory of your
Web application.
2.
Add a filter declaration. The filter element declares a filter, defines a name for the filter, and specifies the Java class that executes the filter. The filter element
must directly follow the context-param element and directly precede the listener and servlet elements. For example:
context-paramParamcontext-param filter
icon small-iconMySmallIcon.gifsmall-icon
large-iconMyLargeIcon.giflarge-icon icon
filter-namemyFilterfilter-name display-nameMy Filterdisplay-name
descriptionThis is my filterdescription filter-classexamples.myFilterClassfilter-class
filter listenerListenerlistener
servletServletservlet
The icon, description, and display-name elements are optional.
3.
Specify one or more initialization attributes inside a filter element. For example:
filter icon
small-iconMySmallIcon.gifsmall-icon large-iconMyLargeIcon.giflarge-icon
icon filter-namemyFilterfilter-name
display-nameMy Filterdisplay-name descriptionThis is my filterdescription
filter-classexamples.myFilterClassfilter-class init-param
param-namemyInitParamparam-name param-valuemyInitParamValueparam-value
init-param filter
Your Filter class can read the initialization attributes using the FilterConfig.getInitParameter or
FilterConfig.getInitParameters methods.
4.
Add filter mappings. The filter-mapping element specifies which filter to execute based on a URL pattern or servlet name. The filter-mapping element
must immediately follow the filter elements.
–
To create a filter mapping using a URL pattern, specify the name of the filter and a URL pattern. URL pattern matching is performed according to the rules
specified in the Servlet 2.4 Specification at http:java.sun.comproductsservletdownload.htmlspecs
, in section 11.1. For example, the following filter-mapping maps
myFilter to requests that contain myPattern. filter-mapping
filter-namemyFilterfilter-name url-patternmyPatternurl-pattern
filter-mapping
14-4 Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server
– To create a filter mapping for a specific servlet, map the filter to the name of a
servlet that is registered in the Web application. For example, the following code maps the myFilter filter to a servlet called myServlet:
filter-mapping filter-namemyFilterfilter-name
servlet-hamemyServletservlet-name filter-mapping
5.
To create a chain of filters, specify multiple filter mappings. For more information, see
Section 14.3.2, Configuring a Chain of Filters .