Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to externalize context-param values in web.xml

I'm tryin to migrate some legacy (struts2-based) web application from Jboss to Open-Liberty server, and I'm wondering if there is a way to externalize the values of context-params (or filter init-params) from web.xml, like it is possible with the ${} syntax in server.xml or using mpConfig feature of eclipse microprofile. In the original project the param values were injected in web.xml at build time, using a placeholder substitution but, according to 12-factor 3rd recommendation, I would prefer to set this values outside software, in environment variables, for example. In my specific case I need to configure a servlet filter and a custom taglibrary with environment dependent param values.

I already tried to use the ${} syntax in web.xml, but no luck:

...
  <context-param>
    <param-name>remincl.resource.provider</param-name>
    <param-value>${remincl.resource.provider}</param-value>
  </context-param>
...

the runtime value of the context-param is: "${remincl.resource.provider}" instead of the actual value that is stored in an environment variable.

I think JEE specs doesn't allow this behavior, but I would like to know if open-liberty offers some extra feature to solve this problem. Otherwise I must keep injecting values at build time (or change the configuration strategy of both filter and taglib).

like image 401
Mauro Antonaci Avatar asked Feb 26 '26 21:02

Mauro Antonaci


1 Answers

A JavaEE standard way to accomplish this would be using a javax.servlet.ServletContextListener.

For example:

@WebListener
public class MyServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // Get the context value from wherever is most convenient:
        // System.getProperty(), System.getenv(), MP Config API, etc
        String value = System.getProperty("remincl.resource.provider");
        event.getServletContext().setInitParameter("remincl.resource.provider", value);
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {}

}
like image 88
Andy Guibert Avatar answered Mar 02 '26 14:03

Andy Guibert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!