aspose file tools*
The moose likes Servlets and the fly likes Refactoring -- extracting hard-coded paths into a properties file for servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Refactoring -- extracting hard-coded paths into a properties file for servlets" Watch "Refactoring -- extracting hard-coded paths into a properties file for servlets" New topic
Author

Refactoring -- extracting hard-coded paths into a properties file for servlets

Franco Finstad
Greenhorn

Joined: May 02, 2003
Posts: 22
Hi everbody,
I have a basic web-database app build with stand-alone servlets. And I need to pull out a bunch of hardcoded paths into properties files so they can be tunable per development/test/production environments.
What is the best way to do this?
Should I use a properties file or put these params in web.xml somehow?
My idea is to put them in a properties file and at server (websphere) startup load them into a constants interface that will then be avaiable to all servlets.
Please advise.
Thanks!!!
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2579

You can store these paths as context params or init params in the web.xml file and access them in servlets using getInitParameter("param_name").
HTH,
- Manish
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
my move would be,
- using a property file
- and hardcoding the path of that property file in one of the file/servlet only so that every other class can know about it
the reason is- if we use web.xml and getInitParam thing then its webserver dependent and when we dont have much control over the production servers its better to have configurations in our control. here anyways we will prepare copy for deployment and that time we have to just worry about ONE file to change the absolute path depending upon production server than spoiling relations with the production webserver admin you know its much easier this way i believe and keeps our code 'webserver independent'
i've been using this convention and learned that i did the right choice given hassels of communicating and pinging 'busy' production server admins...
regards
maulin
leo blk
Greenhorn

Joined: Feb 03, 2003
Posts: 6
You may also wanna try out Class.getResource(""), this will not need any hardcode, but it's a bit tricky.
It will return all the locations that the class you are calling the getResource, all you need to know is where you put your config file and what is the return of the getResource from the class you passed and do some touch up.
Just code a simple jsp and do some testing to find out what exactly you need to do.
leo blk
Greenhorn

Joined: Feb 03, 2003
Posts: 6
Forgot one thing, the previous reply is for application class to get path. If you are coding the servlet you can always call
getServletContext().getRealPath("/");
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Originally posted by leo blk:
Forgot one thing, the previous reply is for application class to get path. If you are coding the servlet you can always call
getServletContext().getRealPath("/");

This doesn't work for all Web Containers, especially if you are deploying your application as a war. For example it will work fine under WebLogic if you deploy your web application in exploded format. However, if you deploy your web application as a war then getServletContext().getRealPath("/") returns null. You are much better off using getServletContext().getResource("") to load any properties files.
Having said that... I would opt to use init parameters in the web.xml instead of properties files, unless you are talking about a very large number of properties.
leo blk
Greenhorn

Joined: Feb 03, 2003
Posts: 6
I have not yet tried the war, but thanks for the pointer Chris.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Refactoring -- extracting hard-coded paths into a properties file for servlets