Difference between JspWriter and PrintWriter in Java EE?

Martin Andersson picture Martin Andersson · Nov 2, 2012 · Viewed 19.2k times · Source

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?

Answer

JB Nizet picture JB Nizet · Nov 2, 2012

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