How do I Locate an application properties file

M

MattC

I am working in a Struts web based J2EE environment. My application is
packaged as a web app.

I have application level configuration information that needs to be
kept in a properties file. My problem is how to programmatically
determine the absolute pathname of that properties file.

It seems to me I have two basic options.

1.) I can create the properties file and put it in a "known" location.
For instance the WEB-INF\classes directory. In order to do this I will
need to determine the absolute path name to the WEB-INF directory. For
instance it might be C:\myproject\public_html\WEB-INF. How can I
determine the absolute pathname of the WEB-INF directory?

2.) I can store a relative pathname of the properties file in the
web.xml file. This is a more flexible implementation and so it is
preferred over option #1. In order to do this I need to determine the
physical location of the WEB-INF directory as in option #1. In addition
I need to gain access to the web.xml file.

Can someone tell me how to access the web.xml file from a Struts
application? In other words, I don't have access to the servlet.init()
method and its associated ServletConfig object.

Thanks,
Matt
 
M

Malte

MattC said:
I am working in a Struts web based J2EE environment. My application is
packaged as a web app.

I have application level configuration information that needs to be
kept in a properties file. My problem is how to programmatically
determine the absolute pathname of that properties file.

It seems to me I have two basic options.

1.) I can create the properties file and put it in a "known" location.
For instance the WEB-INF\classes directory. In order to do this I will
need to determine the absolute path name to the WEB-INF directory. For
instance it might be C:\myproject\public_html\WEB-INF. How can I
determine the absolute pathname of the WEB-INF directory?

2.) I can store a relative pathname of the properties file in the
web.xml file. This is a more flexible implementation and so it is
preferred over option #1. In order to do this I need to determine the
physical location of the WEB-INF directory as in option #1. In addition
I need to gain access to the web.xml file.

Can someone tell me how to access the web.xml file from a Struts
application? In other words, I don't have access to the servlet.init()
method and its associated ServletConfig object.

Thanks,
Matt

Below is an outlined example. The idea is to use the webapp.xml file.
Put in a context-param tag, then in your Java code, use the servlet
context to retrieve the information. If you, like I did for the example,
stuff your properties file into /WEB-INF/data, getRealPath() will find
it for you nicely. What you don't want to do is hardcode c:\ or
something ugly. What if someone deploys your application to a real OS
when it would become something line /opt/mywebapps ?

<web-app>

<context-param>
<param-name>DATA_DIR</param-name>
<param-value>/WEB-INF/data</param-value>
</context-param>
...
</web-app>

private File getDataDir() throws ServletException {
ServletContext application = getServletContext();
String dataDir = application.getInitParameter(DATA_DIR_PARAM);

if (dataDir == null || dataDir.length() == 0)
error(FILE_COMP_ID, MISSING_DATA_DIR_PARAM_ERROR,
FacesMessage.SEVERITY_FATAL);

String realDir = application.getRealPath(dataDir);
if (realDir == null)
error(FILE_COMP_ID, CANNOT_ACCESS_DATA_DIR_ERROR,
FacesMessage.SEVERITY_FATAL);

return new File(realDir);
}
 
R

Raymond DeCampo

MattC said:
I am working in a Struts web based J2EE environment. My application is
packaged as a web app.

I have application level configuration information that needs to be
kept in a properties file. My problem is how to programmatically
determine the absolute pathname of that properties file.

Why do you need the absolute pathname? The simplest solution is to put
the .properties file in the classpath and load it via ResourceBundle.
If you do not want a ResourceBundle you can use
ClassLoader.getResource() to get the URL.
It seems to me I have two basic options.

1.) I can create the properties file and put it in a "known" location.
For instance the WEB-INF\classes directory. In order to do this I will
need to determine the absolute path name to the WEB-INF directory. For
instance it might be C:\myproject\public_html\WEB-INF. How can I
determine the absolute pathname of the WEB-INF directory?

ServletContext or ServletConfig has a getRealPath() method that can help
you. However, there is no real need to do it this way.
2.) I can store a relative pathname of the properties file in the
web.xml file. This is a more flexible implementation and so it is
preferred over option #1. In order to do this I need to determine the
physical location of the WEB-INF directory as in option #1. In addition
I need to gain access to the web.xml file.

Can someone tell me how to access the web.xml file from a Struts
application? In other words, I don't have access to the servlet.init()
method and its associated ServletConfig object.

Look into just putting it in the classpath. It's the easiest way and it
works in every environment (web container, EJB, applet, etc.).


HTH,
Ray
 
R

Roedy Green

Why do you need the absolute pathname? The simplest solution is to put
the .properties file in the classpath and load it via ResourceBundle.
If you do not want a ResourceBundle you can use
ClassLoader.getResource() to get the URL.

If it is a read-only properties file, you can put it in the jar. The
problem only comes when you have to create a properties file out of
thin air and it is not clear where a safe place to put it is.
 

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,012
Latest member
RoxanneDzm

Latest Threads

Top