File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Accessing .properties from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Accessing .properties from a servlet" Watch "Accessing .properties from a servlet" New topic

Accessing .properties from a servlet

Manish Malik

Joined: Jan 27, 2001
Posts: 19

How does a web container (like tomcat) handle .properties files for access by servlets?

For example, if a servlet needs to access some stored key-value pairs from a .properties file, where should this file be placed?

For example,

FileInputStream myPropertiesFile = new FileInputStream("myconfig");
Properties myProperties = new Properties();
// access properties from the object here...

Where should this file be placed (for example, in case of tomcat) for the servlet to be able to read it?

Does the location change, if the class that needs to access the properties file is a web service? (Axis under tomcat)


Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
Servlets can accesss properties files in the same way as java apps, the only thing you need to cater for is the new path.

Personally I put my properties file inside WEB-INF/conf . This is a snippet of code I use:

Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

Or, if you don't want to deal with file paths at all, simply put them in the classpath under WEB-INF/classes and load them by class designation as in "normal" Java apps.

[Asking smart questions] [About Bear] [Books by Bear]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

This was just discussed a few days ago.
You might find this thread interesting:

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Accessing .properties from a servlet
It's not a secret anymore!