我正在学习RequestDispatcher,编写一些基本代码。当调用rd.forward()时,控制权(和响应处理)将转发到路径中命名的资源,这里是另一个servlet。但为什么这段代码不会因为之前的out.write()语句而抛出IllegalStateException(虽然我并不想要这个异常)?
我猜我真正想问的是,这些out.write()语句何时或如何被提交?
谢谢, 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();
}
}
`out.flush();
out.write("exception code?"); out.close();` - Jeff Levineforward
方法之前调用了flush
,则会抛出异常。 - Tala