Difference between JspWriter and PrintWriter in Ja

2019-02-02 09:54发布

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.

The documentation for JspWriter and PrintWriter says there are two differences: 1. JspWriter can throw exceptions, PrintWriter should not do so. 2. 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 - whatever that mean in the context of a JSP page. I'm not sure I've understood this last part. Consider this JSP page:

<%@page import="java.io.PrintWriter"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JspWriter versus PrintWriter</title>
    </head>
    <body>
        <p>I should be row one.</p>
        <%
            out.println("<p>JspWriter said: I should be the second row.</p>");

            PrintWriter pw = response.getWriter();
            pw.println("<p>PrintWriter said: I should be the third row.</p>");
        %>
        <p>I should be the fourth row.</p>
    </body>
</html>

It produces the following output:

PrintWriter said: I should be the third row.
I should be row one.
JspWriter said: I should be the second row.
I should be the fourth row.

As you can see, the JspWriter outputs his string to the browser were I expected it to. But PrintWriter outputs his string before all else is sent to the browser. If we examine the source code sent to the browser, the PrintWriter's string is sent as the very first line, before the DOCTYPE declaration. So in the example above, exactly what happens?

2条回答
老娘就宠你
2楼-- · 2019-02-02 10:30

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
查看更多
甜甜的少女心
3楼-- · 2019-02-02 10:37

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.

查看更多
登录 后发表回答