3

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

2 Answers 2

4

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried your solution: initially it seemed not working as intended (the value was not overwritten) but this was because you can't overwrite a context-param or a filter init-param (the method setInitParameter() returns false if it was not set because this ServletContext already contains a context initialization parameter with a matching name) and, in my case, it was already defined in web.xml with the placeholder value. So I simply removed the declaration in web.xml and it worked.
0

I tried similar way to modify context-param but setInitparameter() is not including new parameter.

trial 1: I tried removing already configured param in web.xml than its throwing Could not open ServletContext resource [/WEB-INF/applicationContext.xml]"}}"

trial 2: Removed ContextLoaderListener from web.xml than it throw java.lang.IllegalStateException

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.