且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java EE中JspWriter和PrintWriter的区别?

更新时间:2022-01-13 06:58:40

解释在你自己的问题中:

The explanation is in your own question:


JspWriter在场景后面使用PrintWriter,但由于默认情况下
JSP页面被缓冲,因此在
缓冲区之前不会创建PrintWriter刷新

JspWriter uses a PrintWriter behind the scene, but since by default JSP pages are buffered, the PrintWriter won't be created until the buffer is flushed

这意味着写入JspWriter的内容被缓冲,并且刷新此缓冲区后(因为缓冲区已满) ,或者因为JSP已经到了执行的末尾),内容被写入响应的PrintWriter。

This means that what is written to the JspWriter is buffered, and once this buffer is flushed (either because the buffer is full, or because the JSP has reached the end of its execution), the contents is written to the response's PrintWriter.

所以你的例子的流程如下:

So the flow of your example is the following one:


  • 静态HTML代码,直到scriptlet:写入内存缓冲区

  • out.println( ...):写入内存缓冲区

  • pw.println(...):写入响应

  • 静态HTML代码直到JSP结束:写入内存中ffer

  • 刷新内存缓冲区:它包含的所有内容都写入响应

  • static HTML code until the scriptlet: written to the in-memory buffer
  • out.println(...): written to the in-memory buffer
  • pw.println(...): written to the response
  • static HTML code until the end of the JSP: written to the in-memory buffer
  • flush of the in-memory buffer: all it contains is written to the response