open a file outside JSP

N

nashaccount

Hi all,

I wonder if any of you have done some programming just to open a file
from a JSP. My JSP code is inside a .war file and the file that I
want to open is outside the war file.

I'll really appreciate any help.



David.
 
L

Lew

nashaccount said:
I wonder if any of you have done some programming just to open a file
from a JSP. My JSP code is inside a .war file and the file that I
want to open is outside the war file.

Where "outside the war [sic] file" is the file you want to open?

It kills application portability to hard-code path references into your
source. You should put the location of the file you want to open in your
deployment descriptor or some such resource (property file, etc.).

You can open arbitrary files with getResource() or getResourceAsStream().

If the file is within your application (i.e., inside a resource directory
underneath the application context root) things are much easier - you can use
relative paths.

<http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String)>
<http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)>

<http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)>
<http://java.sun.com/javase/6/docs/a...er.html#getResourceAsStream(java.lang.String)>

<http://java.sun.com/javaee/5/docs/a...letContext.html#getResource(java.lang.String)>
<http://java.sun.com/javaee/5/docs/a...xt.html#getResourceAsStream(java.lang.String)>

Each of the three variants of these methods has a slightly different idea
about the classpath (the search path by which it finds the resource).
 
N

nashaccount

Thanks for your reply Lew.

My file is located at: c:\reports. When in production (UNIX), my file
will be located at: /reports/

So as you've mentioned before I can't hardcore though.


Regarding the links that you've just sent to me, isn't ClassLoader
only useful to find out classes?


I appreciate,


D.
 
L

Lew

nashaccount said:
Thanks for your reply Lew.

My file is located at: c:\reports. When in production (UNIX), my file
will be located at: /reports/

How are you guaranteeing this?

The regular File (and FileXxxStream, File{Reader,Writer}) classes work with
full paths. The methods I mentioned are for finding resources relative to
your application's location.

Relative locations are *much* easier to manage and guarantee, and are much
more portable. (Likely portability is not a requirement for you, reading
between the lines. But it is generally a bad idea to make non-portable Web apps.)
So as you've mentioned before I can't hardcore though.

Assuming you mean "hardcode", why not? I mean, I know I said you shouldn't,
but I never said you can't.

That said, go the deployment descriptor or properties file route - it provides
portability while still allowing full-path references.
Regarding the links that you've just sent to me, isn't ClassLoader
only useful to find out classes?

No. Didn't you read the Javadocs that I linked for you? If not, do that now.

For your needs, java.io.FileXxx classes may be better. Check out their
Javadocs and see if they suit. The combination of a configuration file
specifying, say, "C:/reports/myreport.log" and code using FileWriter is pretty
powerful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top