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!!!
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
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.
Joined: Feb 03, 2003
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("/");
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.
Joined: Feb 03, 2003
I have not yet tried the war, but thanks for the pointer Chris.