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

S

Sven Jacobs

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!
 
J

James McGill

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"
 
T

Thomas Hawtin

Sven said:
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/a...letContext.html#getResource(java.lang.String)

Tom Hawtin
 
T

Thomas Hawtin

James said:
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
 
J

James McGill

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?
 
S

Sven Jacobs

Thomas said:
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 :)
 
C

Chris Smith

James McGill said:
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
 
H

hiwa

Properties files should be stored in the WEB-INF/classes directory.
The directory is default class path for server class loader.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top