Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Franco Finstad
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not yet tried the war, but thanks for the pointer Chris.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic