How to Change init-parameters at Runtime?
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.
It's called init-parameter
for a reason. So, you can't.
But you can change values at runtime, that's no problem.
- After reading the init parameters put them as attributes of the
ServletContext
(ctx.setAttribute("name", value)
) - Create a small (password-protected) page that lists all attributes of the
ServletContext
and gives the ability to change them.
Maybe you could use apache commons configuration, specifically have a look at Automatic Reloading...
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");
精彩评论