5.3  Handling Mixed Content using a Servlet Input Stream 
Forms sometimes require the uploading of files in addition to regular parameter data. An 
example is the application form of the case study presented in Section 4. In addition to entering 
form fields, the user has the option to submit a drawing file. 
Instead of a simple  post  request, which is usually used to submit form data, the CGI 
 multipart/form data  encoding type has to be used to allow uploading of files through a stream.  
Unfortunately, the servlet API V2.2 does not support multipart/form data requests very well. The 
input stream has to be parsed manually for the parameter value pairs and for the files contained 
in the stream. It turns out that this is quite complex to program   therefore, it makes sense to 
use an existing class library to perform this task. 
A couple of libraries exist, but not all of them are free to use. For the case study presented in 
Section 4, Jason Pell's MultipartRequest class was used, which was published under the GPL 
(the latest version is published under the LGPL). The MulitpartRequest class is available for 
download at 
, including source code and 
documentation. With this class, the multipart/form data request can be handled in a very similar 
way to a regular post request. A simple example consisting a HTML form and the corresponding 
servlet is given below. Please make sure that the upload directory exists. 
This is the HTML form. 
MultipartRequest Demo

MultipartRequest Demo

Please enter your name:

File to upload:


Java Web Hosting Application Development Using Java Technologies Hosting

TotalRoute.net Business web hosting division of Vision Web Hosting Inc. All rights reserved.