Extending Servlet Code

Servlet Filters in the VSM

Contents

The Virtual Shopping Mall (VSM) sample application implements servlet filters. These filters add features to the servlet-based application, and they can be customized and extended without touching the existing servlet code. This article describes how the VSM uses servlet filters to implement the following features:

  • Hit Counter
  • File Upload
  • Access Control

About Servlet Filters

The Java Servlet 2.3 specification introduced the concept of filters, components that can intercept and modify requests to and responses from servlets and JSPs (JavaServer Pages). You can extend and enhance an application by adding one or more filters, and you can apply filters individually or in a series called a filter chain.

From a developer's point of view, a filter is a Java class that implements the interface defined in javax.servlet.Filter. The key method is doFilter, which has the following signature:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

As you can see, doFilter provides access to the request and response objects, and to the filter chain. This method does the actual filtering, and can be called any number of times. The Filter interface also defines an init method, called once before a filter goes into service, and a destroy method, called once before the filter is taken out of service.

The following figure shows the request/response flow in three scenarios: an application with a client (C) and a servlet (S) but no filters, the same application using one filter (F), and again using two filters (F1 and F2) in a filter chain. Note that while a filter can modify a request or a response object, it doesn't have to. A filter could pass the object along unmodified. Also, you don't need a servlet at the back end to use filters—you can configure an application server (or other container) to apply filters to any request.

No Filters One Filter Two-Filter Chain
No Filters One Filter (F) Two-Filter Chain

In addition to implementing the filter, you must edit the Web application deployment descriptor file (web.xml) so the container can find and invoke the filter. Following is a portion of a web.xml file that declares a simple filter, the Java class that implements it, and a URL pattern to which the filter will be applied. The filter name, defined by the developer, maps the Java class to the URL pattern. In this example, the filter named FilterAllRequests is implemented in mypackage1.FilterOne.java, and the container will apply it to all requests it receives (an asterisk is a wildcard in a URL pattern). You can also apply filters to specific directories or resources (files). Other tags are available for declaring filter attributes, but those shown below are essential.

...
  <filter>
<filter-name>FilterAllRequests</filter-name>
<filter-class>mypackage1.FilterOne</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterAllRequests</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> ...

Here is XML code that defines another filter. Because of the value specified in the <url-pattern> element, the container only applies this filter when it receives a request for a resource in the /mydocs directory.

...
  <filter>
<filter-name>FilterMyDocs</filter-name>
<filter-class>mypackage1.FilterTwo</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterMyDocs</filter-name>
<url-pattern>/mydocs/*</url-pattern>
</filter-mapping> ...

To define a filter chain, put two or more filter declarations into the configuration file and supply appropriate values for the <url-pattern> elements . For example, given the code below, the container will apply FilterAllRequests and FilterMyDocs when it receives a request for a resource like http://127.0.0.1:8989/mydocs/foo.html. When two or more filters apply to the same resource, they are invoked in the order that they appear in the configuration file.

...
  <filter>
<filter-name>FilterAllRequests</filter-name>
<filter-class>mypackage1.FilterOne</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterAllRequests</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <filter>
<filter-name>FilterMyDocs</filter-name>
<filter-class>mypackage1.FilterTwo</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterMyDocs</filter-name>
<url-pattern>/mydocs/*</url-pattern>
</filter-mapping> ...

The rest of this article describes how the VSM filters were implemented. The next section describes a filter that counts people who visit the mall.

< Back to Introduction

Questions or comments? Post a message in the OTN Sample Code discussion forum or send email to the author.

Extending Servlet Code: Servlet Filters in the VSM
Author: Robert Hall, Oracle Corporation
Date: June 2002

This document is provided for information purposes only and the information herein is subject to change without notice. Please report any errors herein to Oracle Corporation. Oracle Corporation does not provide any warranties covering and specifically disclaims any liability in connection with this document.
Oracle is a registered trademark and Enabling the Information Age is a trademark or registered trademark of Oracle Corporation. All other company and product names mentioned are used for identification purposes only and may be trademarks of their respective owners.

Oracle

Oracle Corporation
World Headquarters
500 Oracle Parkway
Redwood Shores, CA 94065
U.S.A.

Worldwide Inquiries:
+1.650.506.7200


E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy