I am writing basic code when I find out about RequestDispatcher. I understand that when rd.forward () is called, control (and response processing) is transferred to the resource named in the path - in this case, another servlet. But why doesn't this code throw an IllegalStateException (not what I want) because of the out.write () statements earlier in the code?
I assume I am really asking how and when will these out.write () statements be executed?
Thanks Jeff
public class Dispatcher extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ response.setContentType("text/plain"); PrintWriter out = response.getWriter(); out.write("In Dispatcher\n"); String modeParam = request.getParameter("mode"); String path = "/Receiver/pathInfo?fruit=orange"; RequestDispatcher rd = request.getRequestDispatcher(path); if(rd == null){ out.write("Request Dispatcher is null. Please check path."); } if(modeParam.equals("forward")){ out.write("forwarding...\n"); rd.forward(request, response); } else if(modeParam.equals("include")){ out.write("including...\n"); rd.include(request, response); } out.flush(); out.close(); }
}
source share