Setting context-params in servlet 3.0

If you are using spring boot or working towards java config, you might have or are looking to get away from web.xml based configured web applications. Various frameworks, such as JSF, rely heavily on context parameters for initialization and configuration. If you aren't familiar, < context-param> is available to all parts of the web application and typically can be retrieved by getServletContext().getInitParameter("facelets.DEVELOPMENT");

As you are working at ripping away each piece of the web.xml, one item that wasn't so obvious was how to set < context-param> in java config. The Servlet 3.0 ServletContext API allows for setting init-params, context-params among other things programmatically. One way to get a hook into servlet context in spring is to implement WebApplicationInitializer. WebApplicationInitializer classes are detected automatically by SpringServletContainerInitializer which itself is bootstrapped by a servlet 3.0 container.

Below is snapshot of how to configure context parameters in servlet 3.0 while using spring.

Servlet 2.0Servlet 3.0
<context-param>
    <param-name>facelets.DEVELOPMENT</param-name>
    <param-value>true</param-value>
</context-param>
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application implements ServletContextInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {

        servletContext.setInitParameter(
                "facelets.DEVELOPMENT",
                "true");
    }
}