Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Properties file path hard coded

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written a web application and I would like to read data from a properties file. How can I use a properties file without hard coding the path to the file?
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I often use is the resource bundle and the class name like this.
ResourceBundle bundle = ResourceBundle.getBundle( SomClass.class.getName() );
And it will search for the resource bundle in the classpath.
You cat put SomeClass.properties inside your WEB-INF/classes dir
Another way is hardcode the name of the bundle
getBundle("WebApp");
or
getBundle("configuration.WebApp");
and again put the WebApp.properties inside you WEB-INF/classes dir
or if you use the second approach
WEB-INF/classes/configuration/WebApp.properties
The good thing about ResourceBundle is that the .properties file is searched in the class path, plus a locale strategy is used to loead the rigth bundle ( see class description ) so if your default locale is, let say spanish and you have WebApps_es.properties, this will be loaded
As this file is searched withing the class path you can put the property file in a jar and put it on the lib dir
WEB-INF/lib/compiledClasses.jar
or in the war archive ( myWebApp.war - inside classes or lib again ) allowing you tho have a very portable implemetation.
But if you want to be able to edit this properties file so you can edit login, password information maybe a better approach could be write that information in the web.xml file as a servlet property.
If you don't want to use the ResourceBundle class and still file like Properties aproach, you can write the path in the web.xml file and load it from there
// in a servlet
File propertiesFile = new File( getContext().getConfig().getProperty("property.file") );
// Sorry I don't remember excactly how to query the servlet config in the web.xml file, but you have the idea.

Hope this helps.
 
Scott
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works well. Thanks!!!
 
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hacktorious. You should consider changing your display name according to the javaranch naming policy
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic