Thursday, February 10, 2011

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.

    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.
    From Bozho
  • Maybe you could use apache commons configuration, specifically have a look at Automatic Reloading...

    From pgras
  • 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");
    
    From BalusC

0 comments:

Post a Comment