servletsjakarta-eeweb.xmlopen-liberty

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).


Solution

  • 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) {}
    
    }