Fork me on GitHub

Migrate Servlet Tutorial

This tutorial provides the typical steps in migrating a Servlet / Filter to avoid dependency on JEE. It also enables simpler code that can take advantage of all the OfficeFloor features.

Tutorial Source

Steps to migrate a Servlet

The Servlet to be migrated is taken from the previous tutorial:

public class TutorialServlet extends HttpServlet {

	@Inject
	private InjectedDependency dependency;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.getWriter().write("SERVLET " + this.dependency.getMessage());
	}
}

To migrate a Servlet:

  1. Remove extending HttpServlet
  2. Replace the HttpServletRequest, HttpServletResponse with ServerHttpConnection.
  3. Optionally use parameters for various inputs. Not relevant in above Servlet.
  4. Remove the ServletException. This may involve refactoring to throw another exception. Typically, just let the wrapped exception propagate.
  5. Ensure method is public
  6. Any Servlet init to be made parameters of the method. Not relevant in above Servlet.
  7. Move dependencies to parameters of the method.

The resulting migrated code is as follows:

public class MigratedServlet {

	public void doGet(ServerHttpConnection connection, InjectedDependency dependency) throws IOException {
		connection.getResponse().getEntityWriter().write("SERVLET " + dependency.getMessage());
	}
}

Steps to migrate a Filter

The Filter to be migrated is also taken from the previous tutorial:

public class TutorialFilter extends HttpFilter {

	@Dependency
	private InjectedDependency dependency;

	@Override
	protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		// Determine if filter
		String isFilter = request.getParameter("filter");
		if (Boolean.parseBoolean(isFilter)) {

			// Provide filter response
			response.getWriter().write("FILTER " + this.dependency.getMessage());

		} else {
			
			// Carry on filter chain
			chain.doFilter(request, response);
		}
	}
}

The steps to migrate a Filter follow all those from a Servlet with some extra:

  1. Remove extending HttpFilter
  2. Follow steps in migrating a Servlet. In this case a query parameter is used.
  3. Migrate FilterChain to a @FlowInterface.

The resulting migrated code is as follows:

public class MigratedFilter {

	@FlowInterface
	public static interface ChainNext {
		void doNext();
	}

	public void doFilter(ServerHttpConnection connection, @HttpQueryParameter("filter") String isFilter,
			ChainNext chain, InjectedDependency dependency) throws IOException {

		// Determine if filter
		if (Boolean.parseBoolean(isFilter)) {

			// Provide filter response
			connection.getResponse().getEntityWriter().write("FILTER " + dependency.getMessage());

		} else {

			// Carry on filter chain
			chain.doNext();
		}
	}
}

Next

The next tutorial covers migrating JAX-RS applications.