4

If I modify the XML to change the value of init parameter I see the changes only when web-app is redeployed.

My question is cant I get around this by setting the values at run time.Is there any API that allow me to change the values dynamically.

3 Answers 3

8

It's called init-parameter for a reason. So, you can't.

But you can change values at runtime, that's no problem.

  1. After reading the init parameters put them as attributes of the ServletContext (ctx.setAttribute("name", value))
  2. Create a small (password-protected) page that lists all attributes of the ServletContext and gives the ability to change them.
2

Maybe you could use apache commons configuration, specifically have a look at Automatic Reloading...

2

Make use of properties files instead and write code so that it 1) reads the value from it everytime, or 2) can reload the value on command, or 3) reloads the file automatically at certain intervals.

If you put the properties file somewhere in the webapp's runtime classpath or add its path to the webapp's runtime classpath, then you can easily access/load it as follows:

Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));
String value = properties.get("key");

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