JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

File Upload

Part:   1  2  3  4  5 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

Of course, I also wrote the UploadForm.jsp to present the file upload form page based the settings:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!-- UploadForm.jsp
     Copyright (c) 2004 by Dr. Herong Yang
-->
<jsp:directive.page contentType="text/html"/>
<jsp:declaration><![CDATA[
   private String getItem(String queryString, String key) {
      String value = null;
      if (queryString!=null) {
         int i = queryString.indexOf(key);
         if (i>-1) {
            i = i + key.length();
            int j = queryString.indexOf("&",i);
            if (j>-1) {
               value = queryString.substring(i,j);
            } else {
               value = queryString.substring(i);
            }
            if (value.startsWith("=")) {
               value = value.replaceFirst("=","");
            }
         }
      }
      return value;
   }
]]></jsp:scriptlet>
<jsp:scriptlet><![CDATA[
   String queryString = request.getQueryString();
   String action = getItem(queryString,"action");
   String method = getItem(queryString,"method");
   String enctype = getItem(queryString,"enctype");
   if (enctype.equals("x-www-form-urlencoded"))
      enctype = "application/x-www-form-urlencoded";
   if (enctype.equals("form-data"))
      enctype = "multipart/form-data";
   out.println("<html><body>");
   out.println("<b>File Upload Form</b>:<br/>");
   out.println("<form action="+action+" method="+method
      +" enctype="+enctype+">");
   out.println("Your email:");
   out.println("<input type=text name=email size=20><br/>");
   out.println("Your comments:");
   out.println("<textarea name=comment cols=20 rows=3></textarea>"
      +"<br/>");
   out.println("File 1:");
   out.println("<input type=file name=file1 size=20><br/>");
   out.println("File 2:");
   out.println("<input type=file name=file2 size=20><br/>");
   out.println("<input type=submit name=submit value=Submit><br/>");
   out.println("</form>");
   out.println("</body></html>");
]]></jsp:scriptlet>
</jsp:root>

To these programs, first I copied them to the Tomcat 5.5.4 server on my local machine. Then I run the Internet Explorer 6.0 (IE) with http://localhost:8080/UploadInit.html. I got the form for changing file upload settings:

File Upload Test Settings:

File upload handler: [UploadDump.jsp]
Submit method: [get ]
Encryption type: [application/x-www-form-urlencoded]
Submit

Without changing any values in the form, I clicked the submit button. I got the file upload form:

File Upload Form:

Your email: [herong_yang@yahoo.com    ] 
Your comments: [I am uploading two files:     ]
               [   hello.txt                  ]
               [   dot.gif                    ]
File 1: [                              ] Browse...
File 2: [                    ] Browse...
Submit

Observe that:

  • IE displays a "Browse..." button as part of a FILE type input field.
  • IE ignores the "value=hello.txt" attribute in a FILE type input field. I was expecting "helle.txt" to show up in the input field as the default value.

To continue the test, I used the "Browse..." button, and selected two local files, C:\hello.txt and C:\dot.gif, into "File 1" and "File 2" input fields. Then clicked the Submit button, I got a blank page.

Why? Because the submit method was set to "get". As you remember, the "get" method requires the browser to submit all input fields in the HTTP request header. In fact, if you look at the address field of the browser, you would see the URL encoded input fields appended at the end of the URL:

http://localhost:8080/jsp/UploadDump.jsp?email=herong_yang@yahoo.com&
comment=+I+am+uploading+two+files%3A%0D%0A+++hello.txt%0D%0A+++dot.gi
f&file1=C%3A%5Chello.txt&file2=D%3A%5Cdot.gif&submit=Submit

The previous test was not very interesting. In the next test, I changed the settings on the UploadInit.html page:

File Upload Test Settings:

File upload handler: [UploadDump.jsp]
Submit method: [post]
Encryption type: [multipart/form-data              ]
Submit

Then I selected the same files on the UploadForm.jsp page, and I got the following dump:

-----------------------------7d53495100260
Content-Disposition: form-data; name="email"

herong_yang@yahoo.com
-----------------------------7d53495100260
Content-Disposition: form-data; name="comment"

 I am uploading two files:
   hello.txt
   dot.gif
-----------------------------7d53495100260
Content-Disposition: form-data; name="file1"; filename="C:\hello.txt"
Content-Type: text/plain

Hello world!
-----------------------------7d53495100260
Content-Disposition: form-data; name="file2"; filename="C:\dot.gif"
Content-Type: image/gif

GIF89a   333    !?   ,        D  ;
-----------------------------7d53495100260
Content-Disposition: form-data; name="submit"

Submit
-----------------------------7d53495100260--

The output looks very interesting:

  • IE seems to follow the specifications of RFC 1867 correctly.
  • The "comment" field shows that line breaks in the value of the "textarea" field were preserved correctly.
  • The "file1" and "file2" fields show that the Content-Type header lines were set correctly with text/plain and image/gif.

(Continued on next part...)

Part:   1  2  3  4  5 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - File Upload