Extending Servlet Code

Servlet Filters in the VSM

File Upload Filter

Multipart form for uploading image files.The Virtual Shopping Mall (VSM) application invokes the File Upload filter to process multipart forms. For example, shop owners submit such forms (see figure) to upload pictures of their merchandise so mall customers can browse them. You can do this kind of processing without using filters, but a filter makes the application more flexible because it lets you change parameters such as the destination directory without touching or recompiling the underlying Java code.

Like the other filters in the VSM application, FileUploadFilter is declared in web.xml and implemented as a Java class (in this case, the class name is also FileUploadFilter, but that's not required). The following listing shows the XML code that declares FileUploadFilter. The <init-param> element sets the value of the uploadDir parameter to /images/shops. You can make the application upload files to a different directory simply by changing this value. A listing of the Java code for the FileUploadFilter class (which follows) shows how the uploadDir parameter is used.

...
  <filter>
<filter-name>FileUploadFilter</filter-name>
<filter-class> oracle.otnsamples.vsm.business.FileUploadFilter </filter-class>
<init-param>
<param-name>uploadDir</param-name>
<param-value>/images/shops</param-value>
</init-param>

</filter>
<filter-mapping>
<filter-name>FileUploadFilter</filter-name>
<url-pattern>/webstore/*</url-pattern>
</filter-mapping> ...

Here is a portion of the Java code for the FileUploadFilter class. When the filter is invoked for the first time, its init method executes, querying the config parameter to get the path to the destination directory. The config parameter is a FilterConfig object, and it is initialized by the container, which gets parameter values from the <init-param> element of the filter declaration in the deployment descriptor (web.xml).

Code in the FileUploadFilter.doFilter method queries the request parameter and branches depending on the content type of the request. If the content type is a multipart form, the code invokes a helper class (FileUploadWrapper) and passes a modified request parameter to chain.doFilter. Otherwise, the code calls chain.doFilter without changing the request.

...
  /**
* This method is called by the server before the filter goes into service,
* and here it determines the file upload directory.
* @param <b>config</b> The filter config passed by the container
* @throws <b>ServletException</b>
*/
public void init( FilterConfig config )
throws ServletException {

// Get the name of the upload directory.
try {
java.net.URL uploadDirURL = config.getServletContext().
getResource( config.getInitParameter( "uploadDir" ) );
uploadDir = uploadDirURL.getFile();
} catch( java.net.MalformedURLException ex ) {
throw new ServletException( ex.getMessage() );
} ... /**
* This method performs the actual filtering work. In its doFilter() method,
* each filter receives the current request and response, as well as a
* FilterChain containing the filters that still must be processed. Here
* the doFilter() method examines the request content type, and if it is a
* multipart/form-data request, wraps the request with a FileUpload class.
* @param <b> request </b> The servlet request
* @param <b> response </b> The servlet response
* @param <b> chain </b> Object representing the chain of all filters
* @throws <b>ServletException</b>
* @throws <b>IOException</b>
*/
public void doFilter( ServletRequest request,
ServletResponse response,
FilterChain chain )
throws IOException,ServletException {

HttpServletRequest req = (HttpServletRequest)request;

// Get the content type from the request
String content = req.getHeader( "Content-Type" );

// If the content type is not multipart/form-data, continue
if( content == null || !content.startsWith( "multipart/form-data" ) ) {
chain.doFilter( request, response );
} else {
FileUploadWrapper load = new FileUploadWrapper( req, uploadDir );
chain.doFilter( load, response );
}
}

Once the request has been filtered, utility classes FileUpload and FileUploadWrapper handle the nitty-gritty details of transferring the file from the user's machine to the server. You can view the source code for these classes online.

The next section describes how the VSM uses a filter to control access to application features based on the user's role.

< 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