Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Sven Jacobs - 18 Feb 2006 15:56 GMT
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!

Signature

Sincerely
Sven Jacobs

James McGill - 18 Feb 2006 16:25 GMT
> 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 - 18 Feb 2006 17:34 GMT
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

James McGill - 18 Feb 2006 17:53 GMT
> > servletContext.getRealPath("/") + "/WEB-INF"
>
[quoted text clipped - 3 lines]
>       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?  
Chris Smith - 19 Feb 2006 00:18 GMT
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Thomas Hawtin - 18 Feb 2006 16:41 GMT
> 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
[quoted text clipped - 7 lines]
> 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#getResou
rce(java.lang.String
)

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Sven Jacobs - 18 Feb 2006 18:17 GMT
> 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 :)

Signature

Sincerely
Sven Jacobs

hiwa - 19 Feb 2006 00:25 GMT
Properties files should be stored in the WEB-INF/classes directory.
The directory is default class path for server class loader.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.