Difference between JspWriter and PrintWriter in Java EE?

╄→гoц情女王★ 提交于 2019-11-30 03:57:55

The explanation is in your own question:

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

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:

  • 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

JSPs should use the JspWriter denoted by the “out” implicit object for sending output back to the client. A JspWriter is a buffered version of the PrintWriter. Refer JspWriter API for details. JspWriter also differs from a PrintWriter by throwing java.io.IOException, which a PrintWriter does not.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!