Accessing files from within an application server

Q

Qu0ll

I have some files that reside on an application server (GlassFish) and I
would like to read them from within a servlet. How can I reference them?
Is it a matter of accessing the URL of the files? If so, how can it be done
in such a way that it won't matter which port the server is running on?

For example, perhaps something like:

file://localhost:8080/filename

but what happens if the server is deployed on port 80?

Perhaps it's done using the servlet context?

An example would be nice!

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 
G

GArlington

I have some files that reside on an application server (GlassFish) and I
would like to read them from within a servlet. How can I reference them?
Is it a matter of accessing the URL of the files? If so, how can it be done
in such a way that it won't matter which port the server is running on?

For example, perhaps something like:

file://localhost:8080/filename

but what happens if the server is deployed on port 80?

Perhaps it's done using the servlet context?

An example would be nice!

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)

java.io
use absolute /filename or relative path filename
you do not have to specify the server if the files are local...
 
N

Nigel Wade

Qu0ll said:
I have some files that reside on an application server (GlassFish) and I
would like to read them from within a servlet. How can I reference them?
Is it a matter of accessing the URL of the files? If so, how can it be done
in such a way that it won't matter which port the server is running on?

It's not clear exactly what you are trying to achieve. Do you want to be able to
access the file at a remote client, using the servlet as a "proxy"? Or do you
mean that you want to access a local file within the servlet?

If you want the servlet to serve a local file to a remote client then the URI
the client needs is the URI of your servlet followed by a parameter which would
identify the file in question. The servlet would then respond with the correct
HTML to identify the file MIME type, followed by the file contents.

If you mean that you want to access a local file within the servlet then all you
do is use java.io.File, or any other Java method of accessing local files.
For example, perhaps something like:

file://localhost:8080/filename

No, that won't achieve anything. The file:// URI doesn't take any notice of
host:port. It only accesses the local filesystem so a host and port are
meaningless.
but what happens if the server is deployed on port 80?

From a servlet's POV it doesn't matter. From a client's POV it has to know what
port the server is listening on if it wants to contact it.
 
Q

Qu0ll

It's not clear exactly what you are trying to achieve. Do you want to be
able to
access the file at a remote client, using the servlet as a "proxy"? Or do
you
mean that you want to access a local file within the servlet?

If you want the servlet to serve a local file to a remote client then the
URI
the client needs is the URI of your servlet followed by a parameter which
would
identify the file in question. The servlet would then respond with the
correct
HTML to identify the file MIME type, followed by the file contents.

If you mean that you want to access a local file within the servlet then
all you
do is use java.io.File, or any other Java method of accessing local files.

I want to access a local file in the servlet and use its contents in
determining what to deliver from the servlet. I won't be serving up the
file itself but I need a way of accessing it that will be "portable" as to
wherever the server is actually installed. That is, I don't want to rely on
an absolute path but rather access it from within the WAR file itself.

How do I do that?

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 
O

Owen Jacobson

I want to access a local file in the servlet and use its contents in
determining what to deliver from the servlet.  I won't be serving up the
file itself but I need a way of accessing it that will be "portable" as to
wherever the server is actually installed.  That is, I don't want to rely on
an absolute path but rather access it from within the WAR file itself.

How do I do that?

Use the classloader getResource/getResourceAsStream methods and the
current thread's context classloader to access non-class files that
are in classloader scope. For example, foo.war/WEB-INF/classes/
myConfig.xml is reachable using
Thread.currentThread().getContextClassLoader().getResourceAsStream ("/
myConfig.xml").

-o
 
Q

Qu0ll

Use the classloader getResource/getResourceAsStream methods and the
current thread's context classloader to access non-class files that
are in classloader scope. For example, foo.war/WEB-INF/classes/
myConfig.xml is reachable using
Thread.currentThread().getContextClassLoader().getResourceAsStream ("/
myConfig.xml").

Thanks for the info but I am completely confused by what's happening...

If I use your suggestion and specify "sample.xml" as the parameter to
getResourceAsStream() and place that file in the WEB-INF folder then it can
find it. If I then put the same file in a folder named "xml" under WEB-INF
and pass either "xml/sample.xml" or "/xml/sample.xml" then the file cannot
be found. I am using NetBeans 6.0 and a WAR project if that's relevant.

I realise you actually suggested using "/sample.xml" but that would map to
WEB-INF/classes if you are correct and I don't really want to put
source-type files there as that folder gets blown away by NetBeans on
building.

So what's going on? How do I reference files in WEB-INF/xml/*.xml from
within a servlet?

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 
O

Owen Jacobson

Thanks for the info but I am completely confused by what's happening...

If I use your suggestion and specify "sample.xml" as the parameter to
getResourceAsStream() and place that file in the WEB-INF folder then it can
find it.  If I then put the same file in a folder named "xml" under WEB-INF
and pass either "xml/sample.xml" or "/xml/sample.xml" then the file cannot
be found.  I am using NetBeans 6.0 and a WAR project if that's relevant.

I realise you actually suggested using "/sample.xml" but that would map to
WEB-INF/classes if you are correct and I don't really want to put
source-type files there as that folder gets blown away by NetBeans on
building.

So what's going on?  How do I reference files in WEB-INF/xml/*.xml from
within a servlet?

Include "WEB-INF/" in the name of the resource when locating it.
getResource and friends search from classloader roots, which in the
case of a webapp are:

- the webapp's own root (foo.war/)
- the webapp's classes directory (foo.war/WEB-INF/classes/)
- the root of each JAR file in the lib directory (foo.war/WEB-INF/
lib)

-o
 
Q

Qu0ll

Include "WEB-INF/" in the name of the resource when locating it.
getResource and friends search from classloader roots, which in the
case of a webapp are:

- the webapp's own root (foo.war/)
- the webapp's classes directory (foo.war/WEB-INF/classes/)
- the root of each JAR file in the lib directory (foo.war/WEB-INF/
lib)

That's got it! Thanks muchly :)

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 
L

Lew

Qu0ll said:
That's got it! Thanks muchly :)

There are three resource-loader methods in a JEE app: Class#getResource()
<http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String)>

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

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

plus their getResourceAsStream() variants.

Each class's version of the method has slightly different rules about finding
the root of the provided 'path' depending on whether the argument begins with
a slash or not. For example, the ServletContext version takes a path that
"must begin with a "/" and is interpreted as relative to the current context
root." The others interpret paths relative to the classpath, not the same
thing, and do not require a leading slash. There are emergent consequences to
the way they do it that are not fully documented.
 
Q

Qu0ll

Lew said:
There are three resource-loader methods in a JEE app: Class#getResource()
<http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String)>
ClassLoader#getResource()
<http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)>
and ServletContext#getResource()
<http://java.sun.com/javaee/5/docs/a...letContext.html#getResource(java.lang.String)>
plus their getResourceAsStream() variants.

Each class's version of the method has slightly different rules about
finding the root of the provided 'path' depending on whether the argument
begins with a slash or not. For example, the ServletContext version takes
a path that "must begin with a "/" and is interpreted as relative to the
current context root." The others interpret paths relative to the
classpath, not the same thing, and do not require a leading slash. There
are emergent consequences to the way they do it that are not fully
documented.

OK thanks. What I would really like to be able to do is to get a list of
all available files in a directory under the WEB-INF directory. I can use
getResourceAsStream() on each one but how do I list the available files?

--
And loving it,

-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
 
L

Lew

Qu0ll said:
OK thanks. What I would really like to be able to do is to get a list
of all available files in a directory under the WEB-INF directory. I
can use getResourceAsStream() on each one but how do I list the
available files?

That can be done with a combination of getRealPath() and the File() class, but
why is that useful?

The normal thing is for a program to be aware of its own source code without
having to undergo discovery.

So why is it useful to perform discovery on the WEB-INF?

Wouldn't declarative descriptors (like web.xml or other descriptors) be better?
 

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
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top