Difference between JspWriter and PrintWriter in Java EE?

前端 未结 2 831
囚心锁ツ
囚心锁ツ 2020-12-28 17:35

For all you \"duplicate\" fanatics, there is a similar question on SO right here. The difference is that I paint a vivid example that I can not understand the output of.

相关标签:
2条回答
  • 2020-12-28 18:16

    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
    0 讨论(0)
  • 2020-12-28 18:21

    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.

    0 讨论(0)
提交回复
热议问题