JSP Tutorials - Herong's Tutorial Examples - Version 4.03, by Dr. Herong Yang
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
JSP (JavaServer Pages) Overview
Tomcat 7 Installation on Windows Systems
Syntax of JSP Pages and JSP Documents
JavaBean Objects and "useBean" Action Elements
Managing HTTP Response Header Lines
Non-ASCII Characters Support in JSP Pages
Overview of JSTL (JSP Standard Tag Libraries)
Multiple Tags Working Together
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 2 - POST Method - Successful
Code 4 - Save File - UploadSave.jspx