It is often necessary to upload some file from the client machine to server. There is a special input type file in HTML forms to allow the visitor browser local file system to select the file. When the file is selected it is sent to server as a part of a POST request. During this there are two mandatory restrictions applied to the form with input type file: it must contains attribute enctype set to value multipart/form-data and its method should be POST. Thus, entire request in sent to server in encoded form. JSP container does not parse the content od requests with type multipart. That is why JSP or servlet processing incoming file data has to use own means to handle request and extract a file from there.

There are a lot of free implementations of a multipart-request decoders on the web. We use here utility class javazoom.upload.MultipartFormDataRequest dowloaded within UploadBean from http://www.javazoom.net.

A form with file input for uploaded file selection:

..........
<form action="upload.jsp"
  method="post" enctype="multipart/form-data">

  Select a file:&nbsp;
  <input type="file" name="first" />
  
  <br />
  <input type="submit" name="button" value="upload" />
  
</form>
..........

The page processing request with file encoded:

<%@page contentType="text/html;"%>

<%@page import="java.util.Hashtable"%>
<%@page import="javazoom.upload.MultipartFormDataRequest" %>

...........

<%
  try {
    // decode source request:
    MultipartFormDataRequest data = 
       new MultipartFormDataRequest(request);

    // get the files uploaded:
    Hashtable files = data.getFiles();
    
    if (! files.isEmpty()) {
      // do something with collection of files uploaded;
      ........
    else {
      throw new IllegalStateException("No files supplied");
    }
  catch (RuntimeException error) {

    // set error flag in session:
    request.getSession().setAttribute("error", error);

    // throw its further to print in error-page:
    throw error;
  }
%>
...........