当我了解 RequestDispatcher 时,我正在编写一些基本代码。我知道当调用 rd.forward() 时,控制(和响应处理)被转发到路径中指定的资源 - 在这种情况下是另一个 servlet。但是,由于代码前面的 out.write() 语句,为什么这段代码不抛出 IllegalStateException(不是我想要的)?
我想我真正要问的是,这些 out.write() 语句将如何或何时提交?
谢谢,杰夫
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();
}
}