File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Accessing .properties from a servlet

Manish Malik
Greenhorn

Joined: Jan 27, 2001
Posts: 19
Hi,

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();
myProperties.load(myPropertiesFile);
// access properties from the object here...
myPropertiesFile.close();


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)


Thanks.


Manish
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
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This was just discussed a few days ago.
You might find this thread interesting:
http://www.coderanch.com/t/360641/Servlets/java/keep-configFile-webapp


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing .properties from a servlet