Fork me on GitHub

WoOF within JEE Servlet Container Tutorial

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

Download Tutorial Source

WoofServlet

The WoofServlet is a Servlet 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:

  • WoOF 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 path /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 application. WoOF will also happily co-exist with other frameworks within a Servlet container.

Configuring the WoofServlet

To configure the WoofServlet into an application add the following dependency to the project. The JAR for the dependency contains a web-fragment.xml which will automatically configure the necessary components to run WoOF within the JEE Servlet Container.

		<dependency>
			<groupId>net.officefloor.plugin</groupId>
			<artifactId>officeplugin_woof_servlet</artifactId>
		</dependency>

The resulting web.xml for the example of this tutorial is therefore:

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

However should there be a need to configure the WoofServlet manually, please ensure it is configured as a ServletContextListener to allow it to configure itself. WoOF will only service the requests it is configured to handle and lets 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 the JSP.

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

Template.jsp

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

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

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 CloseableHttpClient client = HttpTestUtil.createHttpClient();
    
    	public void testStaticResource() throws IOException {
    		this.assertRequest("/resource.html",
    				"<html><body>RESOURCE</body></html>");
    	}
    
    	public void testHttpTemplateAndLinkToJsp() throws IOException {
    		this.assertRequest("/template.woof",
    				"<html><body><a href=\"/template-LINK.woof\">TEMPLATE</a></body></html>");
    	}
    
    	public void testLinkToJsp() throws IOException {
    		this.assertRequest("/template-LINK.woof",
    				"<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());
    	}
    
    	@Override
    	protected void tearDown() throws Exception {
    		this.client.close();
    	}
    
    }
    

Next

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