Friday, November 5, 2010

Servlet 3.0 – Add frameworks as Plugins

With Servlet 3.0, it is now possible to include external or 3rd party frameworks as plug-ins to the web application. This is made possible by the ServletContainerInitializer interface. The documentation says that the implementation of this interface - “allows a library/runtime to be notified of a web application's startup phase and perform any required programmatic registration of servlets, filters, and listeners in response to it”. The servlet container finds the ServletContainerInitializer using the JAR services API during application startup. The framework implementing the ServletContainerInitializer needs to add a file named javax.servlet.ServletContainerInitializer in the META-INF/services directory of the JAR file. This file points to the implementation class of the ServletContainerInitializer.The JSF 2 Majorra implementation has used this feature. If we check the JSF-IMPL-2.x jar this file is located in META-INF/services folder and points to the class -  com.sun.faces.config.FacesInitializer. If we look into this class we will find the following lines -

	ServletRegistration reg = servletContext.addServlet("FacesServlet","javax.faces.webapp.FacesServlet");
reg.addMapping("/faces/*", "*.jsf", "*.faces");
servletContext.setAttribute(RIConstants.FACES_INITIALIZER_MAPPINGS_ADDED, Boolean.TRUE);

The ServletContainerInitializer thus registers the faces servlet and three URL mappings out of the box leading to clean web.xml. In fact web.xml is optional in Servlet 3.0.

A note on JBOSS 6 JSF application

JBOSS 6, JSF deployer is very special. It does some tricks with a JSF 2 web application. You can read it here -

No comments:

Post a Comment