Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Properties files in J2EE Sun app server

Status
Not open for further replies.

venomxx

Programmer
Sep 28, 2003
2
US
Hi,
I am trying to find the easiest way to use a Properties object from within the app server that comes with the J2EE 1.3.1 SDK from Sun. I am having s slight problem with obtaining the path to the properties file at deploy time. The app server's security manager is not letting me use the call:

Class.getResource( <resource file> ).getPath(); because local access to the directory is not permitted. The docs don't give much info on how the app server's directory permissions are set but I've narrowed it down to the problem because in the absense of a security manager the call works.

First question is, is there a way to get around the security manager restrictions without opening a potential security hole? The second question, is there a way to obtain the path to the properties file using JNDI instead? Or is there a way that I'm not aware of?

I'm not sure where I can put the properties file to allow local access in the Sun app server.

Any help or links would be apprectiated.

 
Hi,

You can put your properties file in the applications calsspath i.e. WEB-INF/classes directory which is the default classpath in the app server.
And get the InputStream of this properties file by using following method

InputStream in = getClass().getResourceAsStream(&quot;/&quot;+file name );

This will create a InputStream object of the file residing in the classpath.


 
If you put a properties file on your classpath then the java.util.ResourceBundle class will pick it up.

e.g. For a web application I place a webapp_config.properties file in the web app WEB-INF/classes directory deployed on the app server. (The Servlet J2EE spec says that all files under WEB-INF/classes will be on the classpath)

Then, in my servlet code I have

ResourceBundle RB = ResourceBundle.getBundle(&quot;webapp_config&quot;);

The ResourceBundle undergoes a loading process which in the end scans the classpath for a file called webapp_config.properties

Then say to get a var out of the properties file:
String errorMsg = RB.getString(&quot;systemerror.msg&quot;);

Check out the javadocs for this class. Its pretty useful. It does a whole bunch of locale support stuff as well.

RjB.
 
Hi,
I just wanted to say thanks to everyone for the tips. I am up and running. I guess what was compounding the problem was that the deploytool that came with Sun j2ee sdk was not copying my properties file into the web-inf/classes directory when I was deploying. When I originally tried this I assumed that it would be deployed in that directory. However, after reading the tips I looked at the deploy directory and found out I was getting errors because the file wasn't there Doh! I'm not sure if this was an artifact of having .java files in there with the class files since the app server was removing them and copying them to another directory.

But any rate thanks again. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top