[JSF] How to determine the WEB-INF path of the current web application?

Discussion in 'Java' started by Sven Jacobs, Feb 18, 2006.

  1. Sven Jacobs

    Sven Jacobs Guest

    Hello everybody,

    I'm new to the Java world writing my first Java web application with JSF 1.1
    and Tomcat 5.5. I have the following problem:

    I've created a Properties file and placed it in the WEB-INF directory of the
    web application. The file contains several configuration options like
    database parameters. I want to open this file within my web application
    with a FileInputStream which I pass to the load() method of a Properties
    object. Unfortunately I was unable to determine the path of the WEB-INF
    directory of the current web application. I don't want to write the path
    hard coded into the source code.

    Maybe my way is not the correct "Java way" of solving this kind of problem
    so I'm not only interested in an answer to my question but also in
    suggestions how to solve this problem in a better way.

    Thank you!

    --
    Sincerely
    Sven Jacobs
     
    Sven Jacobs, Feb 18, 2006
    #1
    1. Advertising

  2. Sven Jacobs

    James McGill Guest

    Re: [JSF] How to determine the WEB-INF path of the current webapplication?

    On Sat, 2006-02-18 at 16:56 +0100, Sven Jacobs wrote:

    > I was unable to determine the path of the WEB-INF
    > directory of the current web application. I don't want to write the path
    > hard coded into the source code.


    servletContext.getRealPath("/") + "/WEB-INF"
     
    James McGill, Feb 18, 2006
    #2
    1. Advertising

  3. Sven Jacobs wrote:
    >
    > I've created a Properties file and placed it in the WEB-INF directory of the
    > web application. The file contains several configuration options like
    > database parameters. I want to open this file within my web application
    > with a FileInputStream which I pass to the load() method of a Properties
    > object. Unfortunately I was unable to determine the path of the WEB-INF
    > directory of the current web application. I don't want to write the path
    > hard coded into the source code.
    >
    > Maybe my way is not the correct "Java way" of solving this kind of problem
    > so I'm not only interested in an answer to my question but also in
    > suggestions how to solve this problem in a better way.


    The Java way to configure JDBC data sources is through JNDI.

    http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html

    There isn't necessarily any file path to your web application. A server
    might decide to leave all the files in the .war which it has just
    obtained from another server and left in memory, for instance.

    If you want to read a file from the .web app, you can use
    ServletContext.getResource, or similar.

    http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getResource(java.lang.String)

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Feb 18, 2006
    #3
  4. James McGill wrote:
    >
    > servletContext.getRealPath("/") + "/WEB-INF"


    Nooooo!!

    "This method returns null if the servlet container cannot translate
    the virtual path to a real path for any reason (such as when the
    content is being made available from a .war archive)."

    So, that could give you "null/WEB-INF".

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Feb 18, 2006
    #4
  5. Sven Jacobs

    James McGill Guest

    Re: [JSF] How to determine the WEB-INF path of the current webapplication?

    On Sat, 2006-02-18 at 17:53 +0000, Thomas Hawtin wrote:
    > James McGill wrote:
    > >
    > > servletContext.getRealPath("/") + "/WEB-INF"

    >
    > Nooooo!!
    >
    > "This method returns null if the servlet container cannot translate
    > the virtual path to a real path for any reason (such as when the
    > content is being made available from a .war archive)."


    Well, you should assert not null, as ever.

    Regardless of the doc you quoted, it works in Tomcat, indeed from .war
    archives.

    So if this is wrong, what's the right way to do it?
     
    James McGill, Feb 18, 2006
    #5
  6. Sven Jacobs

    Sven Jacobs Guest

    Thomas Hawtin wrote:

    > The Java way to configure JDBC data sources is through JNDI.


    Maybe my example was bad. There are several other settings not related to
    JDBC stored in this file.

    > There isn't necessarily any file path to your web application. A server
    > might decide to leave all the files in the .war which it has just
    > obtained from another server and left in memory, for instance.
    >
    > If you want to read a file from the .web app, you can use
    > ServletContext.getResource, or similar.


    I use getResourceAsStream() and it works, thank you! So this stream from the
    method can actually point to a file within the WAR file, the file in the
    path structure (if available) or from the cache? I honestly don't care as
    long as I can read it :)

    --
    Sincerely
    Sven Jacobs
     
    Sven Jacobs, Feb 18, 2006
    #6
  7. Sven Jacobs

    Chris Smith Guest

    James McGill <> wrote:
    > Well, you should assert not null, as ever.
    >


    I think the point was that by performing the String concatenation, you
    make it difficult to check for the error condition, and postpone the
    resulting incorrect behavior until an unspecified later dat, by which
    time you may have hosed any associated data.

    > Regardless of the doc you quoted, it works in Tomcat, indeed from .war
    > archives.


    Tomcat is unique, in that it silently expands WAR files to a different
    location before it will run the code. Thus, it works there even though
    it doesn't work in the general case. Other web app containers are not
    required to follow Tomcat's lead here.

    > So if this is wrong, what's the right way to do it?


    Best to use ServletContext.getResource, and use the resulting
    InputStream to parse the Properties file.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Feb 19, 2006
    #7
  8. Sven Jacobs

    hiwa Guest

    Re: How to determine the WEB-INF path of the current web application?

    Properties files should be stored in the WEB-INF/classes directory.
    The directory is default class path for server class loader.
     
    hiwa, Feb 19, 2006
    #8
    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. Hans
    Replies:
    3
    Views:
    2,510
    Ryan Stewart
    May 16, 2004
  2. dudu
    Replies:
    0
    Views:
    385
  3. Sebastian Fey

    JSF debugging / JSF newsgroup

    Sebastian Fey, Apr 7, 2006, in forum: Java
    Replies:
    1
    Views:
    7,637
    Danno
    Apr 7, 2006
  4. BlackIce
    Replies:
    1
    Views:
    17,186
  5. =?iso-8859-15?Q?Peter_Kn=F6rrich?=

    Inconsistent float behaviour on "inf" - current state?

    =?iso-8859-15?Q?Peter_Kn=F6rrich?=, Jun 27, 2006, in forum: Python
    Replies:
    1
    Views:
    449
    Scott David Daniels
    Jun 27, 2006
Loading...

Share This Page