relative paths for properties files in a web app

D

Dundonald

I want to plug an API in to a web app. The API has a properties file
that has properties defining the relative location of particular
files. The questions I have are

1. where should I store such 'particular files', indeed including the
properties file itself - should I manually load them in to the WEB-INF
directory?

and

2. how do I relatively relate to the path of the files located inside
the web app (for example in the WEB-INF) inside the properties file.

I hope that makese sense.

Thanks
 
M

Manish Pandit

I want to plug an API in to a web app. The API has a properties file
that has properties defining the relative location of particular
files. The questions I have are

1. where should I store such 'particular files', indeed including the
properties file itself - should I manually load them in to the WEB-INF
directory?

and

2. how do I relatively relate to the path of the files located inside
the web app (for example in the WEB-INF) inside the properties file.

I hope that makese sense.

Thanks

The property file can reside as an application resource in the
classpath. Read up on Resource Bundles in Java for more information
(including APIs like Class.getResourceAsStream()). As far as the path
goes, the files can stay under WEB-INF if they are not be served
directly to the browsers (via HTTP GET requests). If they are to be
published, put them outside of WEB-INF in a folder or may be the app
root.

Hope this helps, as I am not sure if I understood the problem
statement completely.

-cheers,
Manish
 
C

Chris

Dundonald said:
I want to plug an API in to a web app. The API has a properties file
that has properties defining the relative location of particular
files. The questions I have are

1. where should I store such 'particular files', indeed including the
properties file itself - should I manually load them in to the WEB-INF
directory?

and

2. how do I relatively relate to the path of the files located inside
the web app (for example in the WEB-INF) inside the properties file.

I hope that makese sense.

Thanks

You can get at a file in the WEB-INF directory from within a JSP page by
doing this:

<%
String path = application.getRealPath("WEB-INF");
File file = new File(path, "myproperties.properties");
%>

application is a predefined variable in a JSP page, just like request
and response.
 
D

Dundonald

The property file can reside as an application resource in the
classpath. Read up on Resource Bundles in Java for more information
(including APIs like Class.getResourceAsStream()). As far as the path
goes, the files can stay under WEB-INF if they are not be served
directly to the browsers (via HTTP GET requests). If they are to be
published, put them outside of WEB-INF in a folder or may be the app
root.

Hope this helps, as I am not sure if I understood the problem
statement completely.

-cheers,
Manish

Thanks Manish.

I have created a properties resource folder and told WSAD to store
'compiled' (of course they're not compiled) version of properties in
to WEB-INF. So properties file x is in WEB-INF.

The final outstanding question is if for example inside properties
file x a line there is a line such as :

resource_name=./some_file

does that mean that the relative path ./ insinuates that some_file
should be also inside the WEB-INF directory? Does that make sense?

Thanks
 
D

Dundonald

Dundonaldwrote:




You can get at a file in the WEB-INF directory from within a JSP page by
doing this:

<%
String path = application.getRealPath("WEB-INF");
File file = new File(path, "myproperties.properties");
%>

application is a predefined variable in a JSP page, just like request
and response.

Thanks Chris.
 
M

Manish Pandit

Thanks Manish.

I have created a properties resource folder and told WSAD to store
'compiled' (of course they're not compiled) version of properties in
to WEB-INF. So properties file x is in WEB-INF.

The final outstanding question is if for example inside properties
file x a line there is a line such as :

resource_name=./some_file

does that mean that the relative path ./ insinuates that some_file
should be also inside the WEB-INF directory? Does that make sense?

Thanks- Hide quoted text -

- Show quoted text -

I would not recommend using relative paths in properties file to look
up resources for a webapp. To give you an example, if I run Tomcat
(installed in c:\jakarta-tomcat) from within Eclipse (installed in c:
\eclipse) and print System.getProperty("user.dir"), it shows c:
\eclipse. The JSP that prints this sits in c:\workspace\myapp\. You
can clearly see all the variables in play here. Best option will be to
use absolute paths for this purpose. If you want context sensitive
paths, then use look up these resources via the classloader
(getResourceAsStream) and use /some_file instead of ./some_file. As
long as some_file is in the classpath, it'll be picked up. I believe
that is how Spring looks up it's configuration files, etc. as well.

-cheers,
Manish
 
D

Dundonald

I would not recommend using relative paths in properties file to look
up resources for a webapp. To give you an example, if I run Tomcat
(installed in c:\jakarta-tomcat) from within Eclipse (installed in c:
\eclipse) and print System.getProperty("user.dir"), it shows c:
\eclipse. The JSP that prints this sits in c:\workspace\myapp\. You
can clearly see all the variables in play here. Best option will be to
use absolute paths for this purpose. If you want context sensitive
paths, then use look up these resources via the classloader
(getResourceAsStream) and use /some_file instead of ./some_file. As
long as some_file is in the classpath, it'll be picked up. I believe
that is how Spring looks up it's configuration files, etc. as well.

Thanks again Manish.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top