• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing .properties from a servlet

 
Manish Malik
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic