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.
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:
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()); } }
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:
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(); } } }
The next tutorial covers migrating JAX-RS applications.