Java RequestDispatcher - 何时提交响应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18227713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RequestDispatcher - when is a response committed?
提问by Jeff Levine
I'm writing some basic code as I learn about the RequestDispatcher. I understand that when rd.forward() is called, control (and response handling) is forwarded to the resource named in the path - in this case another servlet. But why doesn't this code throw an IllegalStateException (not that I want one) due to the out.write() statements earlier in the code?
当我了解 RequestDispatcher 时,我正在编写一些基本代码。我知道当 rd.forward() 被调用时,控制(和响应处理)被转发到路径中命名的资源 - 在这种情况下是另一个 servlet。但是,由于代码中前面的 out.write() 语句,为什么这段代码不抛出 IllegalStateException (不是我想要的)?
I guess what I'm really asking is, how or when would these out.write() statements be committed?
我想我真正要问的是,这些 out.write() 语句将如何或何时提交?
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();
}
}
}
采纳答案by Tala
Because you haven't called flush
.
因为你还没打电话flush
。
Everything will be cleared out before forwarding if you haven't flushed. Otherwise, you'll get an excpetion you expect.
如果您没有刷新,所有内容将在转发之前清除。否则,你会得到你期望的异常。
As in the docs:
For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource.
forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.
对于通过 getRequestDispatcher() 获取的 RequestDispatcher, ServletRequest 对象的路径元素和参数已调整以匹配目标资源的路径。
forward 应该在响应提交给客户端之前调用(在响应正文输出被刷新之前)。如果已提交响应,则此方法将引发 IllegalStateException。响应缓冲区中未提交的输出在转发之前自动清除。
回答by Adeel Ansari
Read the docs.
阅读文档。
Calling
flush()
on thePrintWriter
commits the response.
调用
flush()
上PrintWriter
提交的响应。
Now, as per your curiosity that why there is no IllegalStateException
thrown. It's simply because PrintWriter.flush()
doesn't throw this or any checked exception. And, we know that the response was not committed when rd.forward()
is invoked, since flush()
comes later in the method.
现在,根据你的好奇心,为什么没有IllegalStateException
抛出。这仅仅是因为PrintWriter.flush()
不会抛出此异常或任何已检查的异常。而且,我们知道响应在rd.forward()
调用时没有提交,因为flush()
在方法后面。
回答by Balakrishna
You did not make call to flush() before forward. So it does not shows any exception. If you write flush() before request is forwarded then it will throw exception.
您在转发之前没有调用flush()。所以它没有显示任何异常。如果你在请求被转发之前写了 flush() 那么它会抛出异常。
When We call flush(), then all what was in buffer is sent to the browser and buffer is cleared.
当我们调用flush()时,缓冲区中的所有内容都会发送到浏览器并清除缓冲区。
In following scenario we will get exception.
在下面的场景中,我们会得到异常。
response.getWriter().print('hello...');
response.getWriter().flush();
request.getRequestDispatcher("/index.").forward(request, response);