WoOF within JEE Servlet Container Tutorial

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

Download Tutorial Source

WoofServletFilter

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.

application.woof configuration.

The above configuration provides the following:

  • OfficeFloor dynamic web template
  • Rendering of a JSP (by the Servlet container) as a navigation link from the above web template
  • Utilising a class method example(...) to service the uri /class

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.

web.xml

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.

ExampleClass

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()
				.getEntity());
		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.

Remaining Code

For completeness of this tutorial the remaining code is included below.

Template.woof.html

Has the #{link} for handling by ExampleTemplateLogic.

<html><body><a href="#{link}">TEMPLATE</a></body></html><html><body><a href="#{link}">TEMPLATE</a></body></html>

ExampleTemplateLogic

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() {
	}

}

Template.jsp

JSP run by Servlet Container. WoOF navigates to this JSP in the above configuration.

<html><body>Linked to JSP</body></html>

MockHttpServlet

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");
	}

}

Unit Test

The unit test highlights the servicing of a request by:

  • Servlet container (for request not to be handled by WoOF as per its configuration)
  • WoOF dynamic template
  • Dynamic template link to be serviced by a JSP from the Servlet container
  • Request being serviced by a class
    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());
    	}
    
    }
    

Next

The next tutorial looks at making use of existing Java Server Pages (JSP).