wood burning stoves 2.0*
The moose likes Servlets and the fly likes .properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark ".properties file" Watch ".properties file" New topic
Author

.properties file

Lidia Cyc
Greenhorn

Joined: Aug 05, 2003
Posts: 25
hi,
i use a .properties file to load the database preferences into an object which connects to db. the object is used by a servlet, and therefor is found in $CATALINA_HOME/webapps/XYZ/WEB-INF/classes/ folder.
the .properties file should be stored in classpath, but on winxp machine nothing but putting it in the same .../classes folder works for me.
the problem is that .../classes folder is visible for everyone. of corse i can make servlet mappings to prevent a possibility to open the .properties file, but still, sounds bad to place a file with the full info regarding my db (including user : pass) in the folder visible to everyone.
what should i do?
10x
[ September 28, 2003: Message edited by: Cyc Lid ]
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
The WEB-INF directory is not visible to anyone.
From the Servlet API Specification:
No file contained in the WEB-INF directory may be served directly to a client by the container.

HTH
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

If you put the .properties file in the WEB-INF folder or in any subfolder of the WEB-INF folder you can use the servletcontext to open the file. You would do something like:
stream = servletContext().getResourceAsStream("/WEB-INF/pathtofile/file.properties");
And I believe that returns an InpuStream so then you can do your typical
properties.load(stream);
And all should be fine.
Now, just to give other options, when working with Web Apps, Connection Pooling is a much better method for getting a connection to a database.


GenRocket - Experts at Building Test Data
Lidia Cyc
Greenhorn

Joined: Aug 05, 2003
Posts: 25
1. i didn't say the file is in WEB-INF, it's in WEB-INF/classes, the folder where you put all your servlets classes, which IS the folder the cpntainer services for requests.
2. of course i'm using a connection pool, but it still needs some preferences to be initialized. i also use
getClass().getResourceAsStream(filename) as you said, which loads the properties file, but only if it's in WEB-INF/classes too...
any other ideas?
10X
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
Originally posted by Cyc Lid:
1. i didn't say the file is in WEB-INF, it's in WEB-INF/classes, the folder where you put all your servlets classes, which IS the folder the container services for requests.

You don't get the point: WEB-INF is not a public node. /classes is in WEB-INF, so is not a public directory. It is used to put their contents available for the application class loader.

2. of course i'm using a connection pool, but it still needs some preferences to be initialized. i also use
getClass().getResourceAsStream(filename) as you said, which loads the properties file, but only if it's in WEB-INF/classes too...
any other ideas?
10X

If you read again Gregg's post you will notice he said:
servletContext().getResourceAsStream("/WEB-INF/pathtofile/file.properties");
nor getClass.getResourceAsStream(filename)
And if you really use connection pooling, you dont need any configuration file in the /classes directory, you can put the init values in the deployment descriptor.
HTH
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

And if you really use connection pooling, you dont need any configuration file in the /classes directory, you can put the init values in the deployment descriptor.
I second that
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: .properties file