Accessing files from within an application server

Discussion in 'Java' started by Qu0ll, Feb 20, 2008.

  1. Qu0ll

    Qu0ll Guest

    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
    _________________________________________________

    (Replace the "SixFour" with numbers to email me)
     
    Qu0ll, Feb 20, 2008
    #1
    1. Advertising

  2. Qu0ll

    GArlington Guest

    On Feb 20, 12:33 pm, "Qu0ll" <> wrote:
    > 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
    > _________________________________________________
    >
    > (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...
     
    GArlington, Feb 20, 2008
    #2
    1. Advertising

  3. Qu0ll

    Nigel Wade Guest

    Qu0ll wrote:

    > 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.

    >
    > Perhaps it's done using the servlet context?
    >
    > An example would be nice!
    >


    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
     
    Nigel Wade, Feb 20, 2008
    #3
  4. Qu0ll

    Qu0ll Guest

    "Nigel Wade" <> wrote in message
    news:fphbjf$8av$...

    > 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
    _________________________________________________

    (Replace the "SixFour" with numbers to email me)
     
    Qu0ll, Feb 20, 2008
    #4
  5. On Feb 20, 10:50 am, "Qu0ll" <> wrote:
    > "Nigel Wade" <> wrote in message
    >
    > news:fphbjf$8av$...
    >
    >
    >
    > > 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?


    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
     
    Owen Jacobson, Feb 20, 2008
    #5
  6. Qu0ll

    Qu0ll Guest

    "Owen Jacobson" <> wrote in message
    news:...

    > 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
    _________________________________________________

    (Replace the "SixFour" with numbers to email me)
     
    Qu0ll, Feb 22, 2008
    #6
  7. On Feb 22, 12:34 am, "Qu0ll" <> wrote:
    > "Owen Jacobson" <> wrote in message
    >
    > news:...
    >
    > > 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?


    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
     
    Owen Jacobson, Feb 22, 2008
    #7
  8. Qu0ll

    Qu0ll Guest

    "Owen Jacobson" <> wrote in message
    news:...

    >> 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)


    That's got it! Thanks muchly :)

    --
    And loving it,

    -Q
    _________________________________________________

    (Replace the "SixFour" with numbers to email me)
     
    Qu0ll, Feb 22, 2008
    #8
  9. Qu0ll

    Lew Guest

    Qu0ll wrote:
    > "Owen Jacobson" <> wrote in message
    > news:...
    >
    >>> 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)

    >
    > 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/api/javax/servlet/ServletContext.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.

    --
    Lew
     
    Lew, Feb 22, 2008
    #9
  10. Qu0ll

    Qu0ll Guest

    "Lew" <> wrote in message
    news:eek:...
    > Qu0ll wrote:
    >> "Owen Jacobson" <> wrote in message
    >> news:...
    >>
    >>>> 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)

    >>
    >> 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/api/javax/servlet/ServletContext.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.
    >
    > --
    > Lew


    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
    _________________________________________________

    (Replace the "SixFour" with numbers to email me)
     
    Qu0ll, Feb 23, 2008
    #10
  11. Qu0ll

    Lew Guest

    Qu0ll wrote:
    > 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?

    --
    Lew
     
    Lew, Feb 23, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin
    Replies:
    3
    Views:
    788
    Alvin Bruney [MVP]
    Apr 19, 2004
  2. Replies:
    0
    Views:
    602
  3. Blake Essing
    Replies:
    1
    Views:
    621
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Sep 11, 2006
  4. mark4asp
    Replies:
    2
    Views:
    2,242
    Harry Haller
    Nov 7, 2006
  5. Replies:
    8
    Views:
    138
Loading...

Share This Page