Accessing File System from JSP Pages

This section provides a tutorial example on how to access the file system from JSP pages using 3 different reference points: Current Directory, Physical Root Directory, and Virtual Root Directory.

The next area I want to test is about how to generate non-HTML response body and set response header lines to match the body. But before writing any testing JSP pages, I need to figure out how to read files on the Tomcat server within JSP pages in order to build non-HTML response body.

Reading files on the Tomcat server requires a good understanding of the file system from the JSP page point of view. With the default configuration, the Tomcat 7 server gives you 3 path name reference points into the file system:

To find out where are those reference points located in the file system, I wrote this JSP tutorial example, CheckPathName.jspx:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- CheckPathName.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
 <jsp:directive.page session="false" import="java.io.*" />
 <jsp:scriptlet><![CDATA[
   String text = "<html><body><pre>";

   try {
      File d = new File(".");
      text += "\n Current Directory: "+d.getAbsolutePath();
      text += "\n Can Execute: "+d.canExecute();
      text += "\n Can Read: "+d.canRead();
      text += "\n Can Write: "+d.canWrite();
      text += "\n";
 
      File p = new File("\\");
      text += "\n Physical Root Directory: "+p.getAbsolutePath();
      text += "\n Can Execute: "+p.canExecute();
      text += "\n Can Read: "+p.canRead();
      text += "\n Can Write: "+p.canWrite();
      text += "\n";

      File v = new File(application.getRealPath("/"));
      text += "\n Virtual Root Directory: "+v.getAbsolutePath();
      text += "\n Can Execute: "+v.canExecute();
      text += "\n Can Read: "+v.canRead();
      text += "\n Can Write: "+v.canWrite();
      text += "\n";
   } catch (Exception e) {
      text += "\n Exception: "+e.toString();
   }

   text += "</pre></body></html>";
   response.setContentType("text/html;charset=UTF-8");
   response.setContentLength(text.length());
   out.print(text);
 ]]></jsp:scriptlet>
</jsp:root>

Deploy this JSP page to the default application on the Tomcat 7 server, and visit it with a Web browser, you should see the following output:

 Current Directory: C:\Users\herong\.
 Can Execute: true
 Can Read: true
 Can Write: true

 Physical Root Directory: C:\
 Can Execute: true
 Can Read: true
 Can Write: true

 Virtual Root Directory: C:\local\apache-tomcat-7.0.32\webapps\ROOT
 Can Execute: true
 Can Read: true
 Can Write: true

Based on the output, we can conclude that:

Note that on older versions, for example Tomcat 5, "Current Directory" is actually having the same location as "Virtual Root Directory". So if you have old JSP pages that use "Current Directory", you might have problems upgrading to Tomcat 7.

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

 What Is HTTP Response?

 HTTP Response Header Lines

 Controlling Response Header Lines

 Viewing Response Header Lines

 Response Header Lines of Static Files

 Response Header Lines Controlled by "page" Directive

 Response Header Lines Controlled by response Object

Accessing File System from JSP Pages

 Returning non-HTML Response Body

 Returning Attachments for Web Download

 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

 Outdated Tutorials

 References

 PDF Printing Version