WoOF within JEE Servlet Container Tutorial
This tutorial demonstrates running an OfficeFloor web application within a JEE Servlet container.
The WoofServletFilter is a Servlet Filter which provides the necessary hooks for running WoOF within a JEE Servlet container.
The following is the configuration for this tutorial.

The above configuration provides the following:
For further explanation of the above WoOF configuration see the previous tutorials.
Within a JEE Servlet Container WoOF defers to the JEE Servlet Container to provide the resources. The ability to link to Servlet container resources (e.g. JSPs, Servlets, static pages, etc) is provided to allow introducing WoOF into JEE web applications without having to rewrite the web applications. WoOF will also happily co-exist with other frameworks within a Servlet container.
The following highlighted configuration adds the WoofServletFilter to the web.xml for running WoOF within the Servlet container.
<?xml version="1.0" encoding="UTF-8"?> <web-app> <filter> <filter-name>WoOF</filter-name> <filter-class>net.officefloor.plugin.woof.servlet.WoofServletFilter</filter-class> </filter> <filter-mapping> <filter-name>WoOF</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>Mock</servlet-name> <servlet-class>net.officefloor.tutorial.servletfilterwebapplication.MockHttpServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Mock</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
WoOF's filter mapping should be /* so that it can intercept each request. WoOF will however only service the requests it is configured to handle and let the Servlet application handle the rest.
As per the above configuration, the following class directly handles a web request.
public class ExampleClass {
public void example(ServerHttpConnection connection) throws IOException {
Writer writer = new OutputStreamWriter(connection.getHttpResponse()
.getBody().getOutputStream());
writer.write("<html><body>CLASS</body></html>");
writer.flush();
}
}
Running within a JEE Servlet container the ServerHttpConnection implementation wraps the ServletRequest and ServletResponse so that existing stand-alone WoOF applications can be run. It is the re-implementing of the base WoOF objects ( ServerHttpConnection , HttpSession ) to use the JEE Servlet Container APIs that the JEE Servlet integration is achieved.
For completeness of this tutorial the remaining code is included below.
Has the #{link} for handling by ExampleTemplateLogic.
<html><body><a href="#{link}">TEMPLATE</a></body></html>
Identifies that the link in the above template should be handled by the LINK output. The above configuration maps this to a JSP for the response.
public class ExampleTemplateLogic {
@NextTask("LINK")
public void link() {
}
}
JSP run by Servlet Container. WoOF navigates to this JSP in the above configuration.
<html><body>Linked to JSP</body></html>
This is included to know when a request is being serviced by the Servlet container (and not OfficeFloor).
public class MockHttpServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("SERVLET");
}
}
The unit test highlights the servicing of a request by:
public class ExampleIT extends TestCase {
private final HttpClient client = new DefaultHttpClient();
public void testUnhandled() throws IOException {
this.assertRequest("/unhandled", "SERVLET");
}
public void testHttpTemplateAndLinkToJsp() throws IOException {
this.assertRequest("/template",
"<html><body><a href=\"/template.links-link.task\">TEMPLATE</a></body></html>");
}
public void testLinkToJsp() throws IOException {
this.assertRequest("/template.links-link.task",
"<html><body>Linked to JSP</body></html>");
}
public void testClass() throws IOException {
this.assertRequest("/class", "<html><body>CLASS</body></html>");
}
private void assertRequest(String uri, String expectedContent)
throws IOException {
// Undertake the request
HttpGet request = new HttpGet("http://localhost:18080" + uri);
HttpResponse response = this.client.execute(request);
assertEquals("Request should be successful: " + uri, 200, response
.getStatusLine().getStatusCode());
// Ensure content is as expected
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
response.getEntity().writeTo(buffer);
assertEquals("Incorrect response entity", expectedContent,
buffer.toString());
}
}
The next tutorial looks at making use of existing Java Server Pages (JSP).