DefaultServletRequestDispatcher implements RequestDispatcher { @Override public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { try (DefaultWebApplicationRequest includedRequest = new DefaultWebApplicationRequest()) { HttpServletRequest originalRequest = (HttpServletRequest) servletRequest; includedRequest.setWebApplication(servletEnvironment.getWebApplication()); includedRequest.setContextPath(originalRequest.getContextPath()); includedRequest.setAttribute(INCLUDE_REQUEST_URI, originalRequest.getRequestURI()); includedRequest.setAttribute(INCLUDE_CONTEXT_PATH, originalRequest.getContextPath()); includedRequest.setAttribute(INCLUDE_SERVLET_PATH, originalRequest.getServletPath()); includedRequest.setAttribute(INCLUDE_PATH_INFO, originalRequest.getPathInfo()); includedRequest.setAttribute(INCLUDE_QUERY_STRING, originalRequest.getQueryString()); includedRequest.setServletPath(path); includedRequest.setDispatcherType(INCLUDE); includedRequest.setPathInfo(null); includedRequest.setQueryString(null); CurrentRequestHolder currentRequestHolder = updateCurrentRequest(originalRequest, includedRequest); invocationFinder.addFilters(INCLUDE, servletInvocation, includedRequest.getServletPath(), ""); try { servletEnvironment.getWebApplication().linkRequestAndResponse(includedRequest, servletResponse); servletInvocation.getFilterChain().doFilter(includedRequest, servletResponse); servletEnvironment.getWebApplication().unlinkRequestAndResponse(includedRequest, servletResponse); } catch (Exception e) { rethrow(e); } finally { restoreCurrentRequest(currentRequestHolder, originalRequest); } } } DefaultServletRequestDispatcher(DefaultServletInvocation servletInvocation, DefaultWebApplication webApplication); void request(DefaultWebApplicationRequest webappRequest, DefaultWebApplicationResponse httpResponse); @Override void forward(ServletRequest servletRequest, ServletResponse servletResponse); @Override void include(ServletRequest servletRequest, ServletResponse servletResponse); void error(String servletName, ServletRequest servletRequest, ServletResponse servletResponse, Throwable throwable);  }
@Test void testInclude() throws Exception { TestWebApplicationRequest request = new TestWebApplicationRequest(); TestWebApplicationResponse response = new TestWebApplicationResponse(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.addServlet("Echo", TestEcho1Servlet.class); webApp.initialize(); webApp.start(); webApp.linkRequestAndResponse(request, response); RequestDispatcher dispatcher = webApp.getNamedDispatcher("Echo"); dispatcher.include(request, response); response.flushBuffer(); String responseText = new String(response.getResponseBytes()); webApp.unlinkRequestAndResponse(request, response); webApp.stop(); assertTrue(responseText.contains("ECHO")); }
@Test void testInclude2() throws Exception { TestWebApplicationRequest request = new TestWebApplicationRequest(); TestWebApplicationResponse response = new TestWebApplicationResponse(); DefaultWebApplication webApp = new DefaultWebApplication(); webApp.addServlet("Echo2", TestEcho2Servlet.class); webApp.initialize(); webApp.start(); webApp.linkRequestAndResponse(request, response); RequestDispatcher dispatcher = webApp.getNamedDispatcher("Echo2"); dispatcher.include(request, response); response.flushBuffer(); String responseText = new String(response.getResponseBytes()); webApp.unlinkRequestAndResponse(request, response); webApp.stop(); assertTrue(responseText.contains("ECHO")); }