Use of a property file in a web application

M

mt

Hello to all,

I'm creating a web application and I wanto to be able to read/store a
value in a property file. My questions are the following:

1- Is the property file supposed to be in a specific location in the
web application? (i.e. inside the WEB-INF folder) Or it doesn't
matter?

2- Right now the property file is inside a package. I'm able to get
the properties using the code:

//name = package where the property file is located (including the
name of the property file) (i.e. com.___.____.....)

final ResourceBundle rb = ResourceBundle.getBundle(name,
Locale.getDefault(),loader);
result = new Properties();
for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
final String key = (String) keys.nextElement();
final String value = rb.getString(key);
result.put(key, value);
}

I want to be able to store a property during the execution of the
application. The code I'm using is:

//name = package where the property file is located (including the
name of the property file) (i.e. com/.../.../.....)
try{

FileOutputStream outputStream = new FileOutputStream(name);
result.store(outputStream, "----No Comment----");
outputStream.close();
}

But the system is not finding the package that has the property file.
The error I'm getting is:
java.io.FileNotFoundException: com\..\...\...\...\project.properties
(The system cannot find the path specified)

To find out what was happening, inside the "catch" block I created a
new file so I could see where the system was searching for that
package. I'm using Eclipse IDE and the file was created in the
eclipse folder (C:\eclipse\newFile.doc). Is there a way that I can
specify where to look for a file inside a webapplication (I want to
search in the tomcat/webapps/myapp/folder -where the application is
located- instead of the /eclipse folder). Or am I supposed to specify
that in the web.xml? I'm new to this so I highly appreciate any help.

-mt
 
H

Heiner Kücker

mt wrote
Hello to all,

I'm creating a web application and I wanto to be able to read/store a
value in a property file. My questions are the following:

1- Is the property file supposed to be in a specific location in the
web application? (i.e. inside the WEB-INF folder) Or it doesn't
matter?

2- Right now the property file is inside a package. I'm able to get
the properties using the code:

//name = package where the property file is located (including the
name of the property file) (i.e. com.___.____.....)

final ResourceBundle rb = ResourceBundle.getBundle(name,
Locale.getDefault(),loader);
result = new Properties();
for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
final String key = (String) keys.nextElement();
final String value = rb.getString(key);
result.put(key, value);
}

I want to be able to store a property during the execution of the
application. The code I'm using is:

//name = package where the property file is located (including the
name of the property file) (i.e. com/.../.../.....)
try{

FileOutputStream outputStream = new FileOutputStream(name);
result.store(outputStream, "----No Comment----");
outputStream.close();
}

But the system is not finding the package that has the property file.
The error I'm getting is:
java.io.FileNotFoundException: com\..\...\...\...\project.properties
(The system cannot find the path specified)

To find out what was happening, inside the "catch" block I created a
new file so I could see where the system was searching for that
package. I'm using Eclipse IDE and the file was created in the
eclipse folder (C:\eclipse\newFile.doc). Is there a way that I can
specify where to look for a file inside a webapplication (I want to
search in the tomcat/webapps/myapp/folder -where the application is
located- instead of the /eclipse folder). Or am I supposed to specify
that in the web.xml? I'm new to this so I highly appreciate any help.

-mt

Its an better way for handling properties files
in the WEB-INF directory of your web app.

The WEB-INF subdir is ascertainable (questable) over

servlet.getServletConfig().getServletContext().getRealPath("/WEB-INF").trim();

Good luck.

Heiner Kuecker
Internet: http://www.heinerkuecker.de http://www.heiner-kuecker.de
JSP WorkFlow PageFlow Page Flow FlowControl Navigation: http://www.control-and-command.de
Java Expression Formula Parser: http://www.heinerkuecker.de/Expression.html
CnC Template Technology http://www.heinerkuecker.de/Expression.html#templ
 
A

anonymous

Heiner said:
mt wrote



Its an better way for handling properties files
in the WEB-INF directory of your web app.

The WEB-INF subdir is ascertainable (questable) over

servlet.getServletConfig().getServletContext().getRealPath("/WEB-INF").trim();

Good luck.

Heiner Kuecker
Internet: http://www.heinerkuecker.de http://www.heiner-kuecker.de
JSP WorkFlow PageFlow Page Flow FlowControl Navigation: http://www.control-and-command.de
Java Expression Formula Parser: http://www.heinerkuecker.de/Expression.html
CnC Template Technology http://www.heinerkuecker.de/Expression.html#templ
I agree.
You can also use web.xml to store a path to a file, or any other
variable you like and read it at runtime.
 
W

Wendy S

mt said:
1- Is the property file supposed to be in a specific location in the
web application? (i.e. inside the WEB-INF folder) Or it doesn't
matter?

Anywhere the system can find it, I put mine in 'WEB-INF/classes' and
retrieve it with:

ClassLoader cl = UniSessionFactory.class.getClassLoader();
InputStream input = cl.getResourceAsStream( propsFileName );

This code works anywhere, (not just in a webapp) as long as the file is
somewhere at the 'top' of the classpath. I think you can also use a package
name, though I haven't tried it. And I don't attempt to update this
..properties file.
 
G

gimme_this_gimme_that

Answer to item 1 :

in WEB-INF/classes
or a subdirectory of it.
You should fetch the properties as a resource, not as a text file.
 

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,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top