Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm just wondering whether to keep some of my variables in Constants class or keep it in web.xml

Say, I want to keep a variable of Facebook graph API prefix or api_key, client_id

From my understand, the difference between and web.xml is web.xml is easier to rewrite on compile using ant.

So, you can replace your variables in web.xml according to what environment you are building you app for. (client_id varies by development environment/production environment, for example)

If I understand it right, then Facebook graph API prefix should be kept in (because it always is "") and api_key, client_id should be kept in web.xml?

What's the proper way to use them?

share|improve this question
up vote 1 down vote accepted

In your scenario, these values are best kept in properties file. Create a properties file in classpath and read it at your application startup to instantiate the values. Or you can read them fresh for each request, if they tends to change. Java provides very easy way for reading/writing properties file with Properties class.

You can also externalize the properties file if you want. i.e. put them into user home directory to make them completely independent. Think of it, when you change the value and the running application takes the new value without re-build or even re-deployment.

share|improve this answer
Thank you. Keeping all of them in one properties file seems to be a good way to handle them. We can have a properties file for each environment and have the application choose properties file based on environment it is running on. – Pongsakorn Semsuwan Nov 7 '12 at 7:08

Well it really depends on how really "constant" they are:

  • if they're true constants that never ever change, not when you change the deployment environment (integration, production, etc.), not when you deploy it at your client's side, than it's perfectly sensible to keep them as Java constants (final fields) in some Java class.

  • if they change with environment, having them as Java constants is obviously bad. Putting them in web.xml is somewhat better, but you'll still have to change something in your project to redeploy to a different environment. What I like to do in these situations is pass the values as system properties. Your app should have a generic "Config" class which is able to recuperate the value of a system property given it's name (via System.getProperty("propName")) and then configure my environment with specific values for those properties (via .bashrc, or via Web App Server start-up arguments).

share|improve this answer
Thank you. This gives me a better understand of using them. :) – Pongsakorn Semsuwan Nov 7 '12 at 7:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.