Fork me on GitHub

JEE Servlet Container within WoOF Tutorial

This tutorial demonstrates running JEE Servlet functionality within a WoOF web application.

Download Tutorial Source

Extending WoOF application for Servlet functionality

The ServletContainerWoofApplicationExtensionService provides extension to a WoOF application by detecting the presence of a web.xml file following standard Maven war project structure or standard JEE Servlet *.war packaging. On detecting this file, it will load a chained servicer containing the configured Servlet functionality.

This occurs automatically by the inclusion of the following dependencies on the class path:

		<dependency>
			<groupId>net.officefloor.plugin</groupId>
			<artifactId>officeplugin_woof_servlet</artifactId>
		</dependency>
		<dependency>
			<!-- Necessary for non-interface JEE dependencies -->
			<groupId>org.eclipse.jetty.orbit</groupId>
			<artifactId>javax.servlet</artifactId>
		</dependency>

The result is that any unhandled HTTP request by the WoOF functionality is sent to this servicer. This servicer attempts to service the HTTP request with the web.xml configured Servlet functionality.

Should the HTTP request not be handled by the Servlet functionality then the last chained servicer is used. The last chained servicer attempts to send a corresponding static resource by URI or a 404 if no corresponding resource.

Servlet functionality

The following provides the configuration of the web.xml Servlet functionality for this tutorial:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>

	<servlet>
		<servlet-name>Example</servlet-name>
		<servlet-class>net.officefloor.tutorial.servletmigration.ExampleHttpServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Example</servlet-name>
		<url-pattern>/example</url-pattern>
	</servlet-mapping>

</web-app>

Servlet functionality supported by the WoOF extension is:

  • Filters
  • Servlets
  • MIME mappings

Note that the intention is not to rebuild another JEE Server. The intention is provide enough Servlet Container functionality to embed existing JEE Servlet Applications within WoOF so that they do not need to be fully migrated to WoOF functionality.

The configured Servlet is as follows:

public class ExampleHttpServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().write("SERVLET");
	}

}

Unit Test

The unit test highlights the servicing of the HTTP request by the Servlet functionality.

	public void testExampleHttpServlet() throws Exception {

		// Start WoOF (with Servlet extension on class path)
		WoofOfficeFloorSource.start();

		// Undertake the request to be serviced by Servlet
		HttpGet request = new HttpGet("http://localhost:7878/example");
		HttpResponse response = this.client.execute(request);
		assertEquals("Ensure request successful", 200, response.getStatusLine()
				.getStatusCode());

		// Ensure response entity is as expected
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		response.getEntity().writeTo(buffer);
		assertEquals("Incorrect response entity", "SERVLET", buffer.toString());
	}

Next

The next tutorial looks at migrating an existing JEE Servlet Application to run within OfficeFloor as a WoOF Application.