Code 3 - Dump File - UploadDump.jspx

This section provides a tutorial example of a JSP page, UploadDump.jspx, that reads the uploaded file and dumps it back to the browser. The key logic is the request.getInputStream() call.

As Part 3 of the file upload test, here is my UploadDump.jspx page, that reads the uploaded file and dumps it back to the browser.

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- UploadDump.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<p>
<jsp:scriptlet><![CDATA[
   out.println("<pre>");
   ServletInputStream in = request.getInputStream();
   byte[] line = new byte[128];
   int i = in.readLine(line, 0, 128);
   while (i != -1) {
      out.print(new String(line, 0, i));
      i = in.readLine(line, 0, 128);
   }
   out.println("</pre>");
]]></jsp:scriptlet>
</p>
</body></html>
</jsp:root>

As you can see, this JSP page is very simple. The key is the request.getInputStream() call, which returns a ServletInputStreem object connecting to the HTTP request body. Reading the data from the stream object and dump it back to the browser is easy.

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

File Upload Test Application

 RFC 1867 - Form-based File Upload in HTML

 Code 1 - Display Options - UploadInit.html

 Code 2 - Display Form - UploadForm.jspx

Code 3 - Dump File - UploadDump.jspx

 Test 1 - GET Method - Failed

 Test 2 - POST Method - Successful

 Code 4 - Save File - UploadSave.jspx

 Test 3 - Save File - Successful

 Code Review - UploadSave.jspx

 Outdated Tutorials

 References

 PDF Printing Version