Fork me on GitHub

WAR Tutorial

This tutorial demonstrates embedding a web archive (WAR) application into OfficeFloor.

The reason for embedding the WAR, is to enable a phased migration to OfficeFloor. Forcing a rewrite of the application into OfficeFloor is typically expensive (plus falls under the anti-pattern of "big bang"). Therefore, the WAR application can be embedded in OfficeFloor and functionality slowly migrated out of it to take advantage of OfficeFloor's IoCC.

Tutorial Source

WAR Application

The example WAR application to embed for this tutorial is located here.

It consists of two servlets. The first is a simple servlet:

@WebServlet(urlPatterns = "/simple")
public class SimpleServlet extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

The second servlet has an injected dependency:

@WebServlet(urlPatterns = "/inject")
public class InjectServlet extends HttpServlet {

	private ServletDependency dependency;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String value = this.dependency != null ? this.dependency.getMessage() : "NO DEPENDENCY";

with the dependency as follows:

public class ServletDependency {

	public String getMessage() {
		return "INJECTED";

Embedding the WAR

To embed a WAR application into OfficeFloor, include the following maven dependency:


Once included, the location of the WAR file must be specified. This is typically undertaken as a system property defined by OfficeFloorWar, i.e. -DOFFICE.war.path="/path/to/war"

Servlet Dependencies

Dependencies for the servlets are provided by OfficeFloor. This may either be through OfficeFloor directly. Or by incorporating a third party dependency injection library, such as Spring.

In the case of this tutorial, OfficeFloor is directly providing the dependency in application.objects:

	<managed-object class="net.officefloor.tutorial.warhttpserver.InjectServletDependency" />

with a custom dependency:

public class InjectServletDependency extends ServletDependency {

	public String getMessage() {
		return "INJECT";

Note that at runtime, the WAR classes are automatically included on the class path. However, for IDE development, the classes of the WAR need to be referenced:


This is made available by flagging the WAR project to produce the classes artifact:



The following tests demonstrate embedding the WAR and invoking each servlet.

	public final MockWoofServerExtension server = new MockWoofServerExtension().property(OfficeFloorWar.PROPERTY_WAR_PATH,
			getWarPath()); // gets location of war file

	public void simpleServlet() {
		MockWoofResponse response = this.server.send(MockWoofServer.mockRequest("/simple"));
		response.assertResponse(200, "SIMPLE");

	public void injectServlet() {
		MockWoofResponse response = this.server.send(MockWoofServer.mockRequest("/inject"));
		response.assertResponse(200, "INJECT");


The next tutorial covers using Servlets / Filters as procedures.