Fork me on GitHub

Servlet Container Tutorial

This tutorial demonstrates the ease of embedding a WoOF application into a Servlet container. This is typically necessary to run with PaaS Cloud providers.

The tutorial will demonstrate this with a simple increment service.

Tutorial Source

Configuring for Servlet container

To run within a Servlet container, add the following dependency:

		<dependency>
			<groupId>net.officefloor.server</groupId>
			<artifactId>officeserver_servlet</artifactId>
		</dependency>

Then package the application as a war to enable being deployed to Servlet container.

Note that WoOF will run as a Filter and only intercept requests it can handle. This means existing Servlet functionality can work side by side with WoOF. Very useful for phased migration of existing Servlet applications to WoOF.

Also, WoOF does not do class path scanning. Therefore, the start times are very fast. This makes WoOF ideal for scale to zero PaaS deployments. See the OfficeFloor Subscription App for an example (code also available https://github.com/officefloor/Subscription).

Integration Test

The following integration test has the project started as follows:

			<plugin>
				<groupId>org.eclipse.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<configuration>
					<httpConnector>
						<port>8999</port>
					</httpConnector>
					<stopKey>quit</stopKey>
					<stopPort>9000</stopPort>
				</configuration>
				<executions>
					<execution>
						<id>start-jetty</id>
						<phase>pre-integration-test</phase>
						<goals>
							<goal>start</goal>
						</goals>
					</execution>
					<execution>
						<id>stop-jetty</id>
						<phase>post-integration-test</phase>
						<goals>
							<goal>stop</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-failsafe-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>integration-test</goal>
							<goal>verify</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

This will allow the following integration test to call the WoOF application within the Servlet container to increment the value:

	private static final String SERVER_URL = "http://localhost:8999";

	@RegisterExtension
	public HttpClientExtension client = new HttpClientExtension();

	private final ObjectMapper mapper = new ObjectMapper();

	@Test
	public void woofInput() throws Exception {

		// Create request
		HttpPost post = new HttpPost(SERVER_URL + "/increment");
		post.addHeader("Content-Type", "application/json");
		post.setEntity(new StringEntity(this.mapper.writeValueAsString(new Increment.Request("1"))));

		// Execute request
		HttpResponse response = this.client.execute(post);

		// Confirm expected response
		String entity = EntityUtils.toString(response.getEntity());
		assertEquals(200, response.getStatusLine().getStatusCode(), "Incorrect status: " + entity);
		assertEquals("application/json", response.getFirstHeader("Content-Type").getValue(), "Incorrect content type");
		Increment.Response entityResponse = this.mapper.readValue(entity, Increment.Response.class);
		assertEquals("2", entityResponse.getValue(), "Incorrect response");
	}

Next

The next tutorial covers REST.