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





PostPosted: 2006-2-18 23:56:00 Top

java-programmer, [JSF] How to determine the WEB-INF path of the current web application? 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
 
James McGill





PostPosted: 2006-2-19 0:26:00 Top

java-programmer >> [JSF] How to determine the WEB-INF path of the current web application? 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"



 
Thomas Hawtin





PostPosted: 2006-2-19 1:01:00 Top

java-programmer >> [JSF] How to determine the WEB-INF path of the current web application? 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





PostPosted: 2006-2-19 1:54:00 Top

java-programmer >> [JSF] How to determine the WEB-INF path of the current web application? 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/
 
 
James McGill





PostPosted: 2006-2-19 1:54:00 Top

java-programmer >> [JSF] How to determine the WEB-INF path of the current web application? 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?

 
 
Sven Jacobs





PostPosted: 2006-2-19 2:18:00 Top

java-programmer >> [JSF] How to determine the WEB-INF path of the current web application? 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
 
 
Chris Smith





PostPosted: 2006-2-19 8:18:00 Top

java-programmer >> [JSF] How to determine the WEB-INF path of the current web application? James McGill <email***@***.com> 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