After going through this How To document,
you should be able to write code to read a file from the file system
from a JSP, in different ways.
Introduction
Typically, most web applications
need to read some application specific data stored in static files in
the file system. However, the programmer should know the full path to these files, to access them using the constructors of I/O classes
. Since a web application is deployed as a WAR on the application servers/
servlet engines, the full path to these files, which are bundled with the
application vary for each server. One cannot use relative file paths, since
the relative path will start from the directory from which the application
server/servlet engine is started, and this will vary with servers.
One technique is to configure
the file paths in web.xml using <init-param> tags, and retrieve
file paths in the servlet/JSP. But this needs changing web.xml before
deployment, to point to the actual paths.
To overcome these configuration
issues, one can use the features of java.lang.ClassLoader and javax.servlet.ServletContext
classes. The remaining part of the document discusses this approach.
Pre-requisites for running the example
To run the sample provided here, you need
Oracle9i
JDeveloper 9.0.3 (Download from OTN) OR
Oracle9iAS Containers for
J2EE (OC4J) 9.0.3 (Download from OTN)
Apache Ant 1.4.x, if you are going to
deploy on OC4J (Download from Apache).
(optional, mandatory only
when JDeveloper is not used)
The example is a Currency Converter program,
which reads a list of currency exchange rates stored in a file
and uses this data to do the conversions.
The files in the example are
A text file, rates.txt,
which stores the exchange rates.
An HTML, selectCurrencies.html
, which allows a user to select the currencies and the amount.
A JSP, convert.jsp, which converts
the currencies and prints the result.
A JSP, reverseRates.jsp, which
reverses the exchange rates and writes to a file reverserates.txt
under WEB-INF/classes
There are various ways of reading a file. The javax.servlet.ServletContext
API has the methods to access resources in general.
getResource(String resource)
-
Returns a URL for the resource in the web application
getResourceAsStream(String resource)
-
Returns an InpuStream for the resource in the web application
getResourcePaths(String path)
-
Returns a directory-like listing of all the paths to resources within
the web application whose longest sub-path matches the supplied path argument.So
in convert.jsp, we use these APIs to retrieve the file
<%
...
// Get the file as an input stream
// The path is relative to the application context, and rates.txt
is placed
// under the WEB-INF folder of the application InputStream is = config.getServletContext().getResourceAsStream("/rates.txt");
...
%>
Or we can use the APIs from ClassLoader, which requires
that the resource to be in the classpath. In this case, we should place
the file under WEB-INF/classses, where all application classes
reside.
The file reverseRates.jsp combines the ClassLoader
approach and the ServletContext approach
<%
...
// Get the URL for the file and create a stream explicitly java.net.URL url =config.getServletContext().getResource("/rates.txt");
BufferedReader breader =new BufferedReader(new InputStreamReader(url.openStream()));
// Yet another way of opening files is by using the ClassLoader API
// A ClassLoader instance is obtained as shown below. This instance
must be the
// one which loads the classes in the application.This is ensured
by getting
// the instance via the current thread object.
// Note that url will be null if the file doesn't exist. We assume
that there
// is an empty file "reverserates.txt" under /WEB-INF/classes
url = Thread.currentThread().getContextClassLoader().getResource("reverserates.txt");
// We will create a BufferedWriter to write the reverse exchange
rates
BufferedWriter writer = new BufferedWriter(new FileWriter(url.getFile()));
...
%>
Running the sample
To run from Oracle9i JDeveloper
Open the file Files-JSP.jws
in Oracle9i JDeveloper
Select Files-JSP.jpr and select
Run. This will deploy the application to embedded OC4J. You can see
a list of currencies that you can exchange.To see the reverse rates, click
on the 'Reverse rates' link.
To run on OC4J,
Start OC4J.
On command prompt , set the environment
variables ANT_HOME,JAVA_HOME,OC4J_HOME and OC4J_HOST