servlet 响应何时提交或刷新?

when is servlet response committed or flushed?

根据 javadoc:

在-request.getRequestDispatcher("/Test").forward(request,response);

forward should be called before the response has been committed to the client (before response body output has been flushed).Uncommitted output in the response buffer is automatically cleared before the forward.

当这个响应被提交或被刷新时我感到困惑?

这是在 printwriterprintln 中写的。

不,不是。 就在您像

这样的代码中手动刷新它时

response.flush().

通常 servlet 容器会在 "your" 方法之后为您完成。

PrintWriter 上调用 flush() 提交响应。

forward 方法允许一个 servlet 对请求进行初步处理,并允许另一个资源生成响应。

转发前可以有多个out.write语句,但不能在转发前调用flush。 喜欢

PrintWriter out = response.getWriter();
out.write("forwarding...\n");
rd.forward(request, response); //this is good

但是如果

out.write("forwarding...\n");
 out.flush();
 rd.forward(request, response); //this throws an exception