O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.2. Using the com.oreilly.servlet Library

Problem

You want to use the com.oreilly.servlet classes that O'Reilly author Jason Hunter has developed to handle file uploads.

Tip

Of course, this isn't much of a problem, as Jason's library takes most of the work out of uploading and accepting files. I use Jason's library here (with his permission, of course) because it handles file uploads nicely, and there's no good reason to reinvent a perfectly good wheel.

Solution

Download the distribution ZIP file from http://www.servlets.com/cos/index.html. Add the cos.jar file, which is part of the distribution to the WEB-INF/lib directory of your web application. Make sure that you adhere to the software license when using the library.

Discussion

A JAR file named cos.jar includes the com.oreilly.servlet and com.oreilly.servlet.multipart packages. These packages include several classes, such as all of the Java classes that begin with "Multipart," which can be used to handle file uploading in a servlet.

Tip

The cos.jar archive also contains many other interesting and useful classes to use with servlets, but the following recipes focus on file uploads.

Download the latest ZIP file containing the distribution from http://www.servlets.com/cos/index.html. The contents of the ZIP file include cos.jar , which you need to add to the WEB-INF/lib directory of your web application. In your servlet, you then import the classes that you want to use:

import com.oreilly.servlet.MultipartRequest; import com.oreilly.servlet.multipart.FileRenamePolicy; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required