Click here to Skip to main content
15,886,046 members
Articles / Programming Languages / Java
Tip/Trick

A Programmatic Approach of Configuring web.xml

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
13 Apr 2015CPOL1 min read 13.1K   2  
Configuring the web.xml for any Java/J2ee project using a programmatic approach

Introduction

This tip briefs about configuring the web.xml for any Java/J2ee project using a programmatic approach.

Background

As a traditional approach, any servlet context and other listeners are configured by making entries in the web.xml file in any Java/J2ee web based application. In contrast to this, the introduction of servlet 3.0 aims to provide a programmatic approach to configure the entries of web.xml.

Any servlet 3.0 based web application can just implement the interface a ServletContainerInitializer interface, which the ServletContext interface will be explicitly passed at the runtime and acts as a representation of the container. This can be used to configure servlet, listeners, any filters and other contents which was normally specified in the typical web.xml file.

With advanced spring framework 3.1 and above, this implementation has been made much easier because it provides an implementation of ServletContainerInitializer known as the SpringServletContainerInitializer. Basically, it registers the implementation through the META-INF/services/java.servlet.ServletContainerInitializer file in the spring web module file. Also, the task of configuring the servlet context, listeners, and filters will be delegated to another interface of spring called WebApplicationInitializer.

The only task for the developer is to implement the WebApplicationInitializer and configure the servlet context in the implementation.

Let us see a sample on how to implement the same.

Using the Programmatic Approach

The typical web.xml for a servlet configuration will be as shown below:

XML
<servlet>
    <servlet-name>dispatcher-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/spring/dispatcher-servlet.xml</param-value>
    </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
 <servlet-name>dispatcher-servlet</servlet-name>
 <url-pattern>/</url-pattern>
</servlet-mapping>

The above mentioned web.xml entry can be transformed into Java code which is of 100% completeness and works well.

Java
public class MyWebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext container) {
    XmlWebApplicationContext appContext = new XmlWebApplicationContext();
    appContext.setConfigLocation("/WEB-INF/spring/dispatcher-servlet.xml");
    ServletRegistration.Dynamic dispatcher = container.addServlet
    	("dispatcher", new DispatcherServlet(appContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
  }
}

Conclusion

With this approach, it can be ensured that the entries made programmatically will be verbose and in case of any exceptions/errors due to the configuration made can be easily debugged to find the root cause.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
A simple passionate guy about java/j2ee development artifacts and web developments.

Comments and Discussions

 
-- There are no messages in this forum --