根据 javadoc:
in-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.
当这个响应被提交或被刷新时我感到困惑?
这是在 printwriter
的 println
中写的。
最佳答案
在 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
关于java - servlet 响应何时提交或刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749730/