File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes access properties file from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "access properties file from servlet" Watch "access properties file from servlet" New topic

access properties file from servlet

Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
What is the best way to determine the path for a properties file from within a servlet please? At the moment I have the path hard-coded inside a loadProps() method with the tomcat\bin as the 'root' like this:
input=new FileInputStream( "../webapps/myWebapp/properties/props" );
but it would be nice if the webapp could work out the relative path of its own context so that it would still work even if the webapp is re-named.
Many thanks,
[ September 18, 2003: Message edited by: Peter Straw ]
Evaristo Ballorca

Joined: Feb 13, 2002
Posts: 17
If we want get the real path where the web application is located we could ask to the servlet context:
servletContext.getRealPath( "/" );
This method will return a real path for a relative path. If we are in a web application context is /webapp_example, this method will return:
Then if you want to acces to the WEB-INF directory you will write:
servletContext.getRealPath( "/" ) + "WEB-INF/";
If you are in a servlet you could get the servlet context doing:
I wait that i help you.
See you
Jason Davies

Joined: Sep 18, 2003
Posts: 14
I would recommend using ServletContext.getResource() or ServletContext.getResourceAsStream().
This allows you to read data from a .war file as well, and will work wherever the webapp is deployed.

<a href="" target="_blank" rel="nofollow"></a> - Web programming articles and tutorials.
Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
great! thanks very much both of you that works perfectly.
I agree. Here's the link:
subject: access properties file from servlet
It's not a secret anymore!