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?