You MUST memorize these methods
You have to know these for the exam. And it’s tricky. Notice that to write to a ServletOutputStream you write(), but to write to a Print-Writer you... println()! It’s natural to assume that you write to a writer, but you don’t. If you already use java.io, then you’ve been down this road. But if you haven’t, just remember:
println() to a PrintWriter
write() to an ServletOutputStream
Make sure you remember that the method names for getting the stream or the writer both drop the first word in the returned type:
You need to recognize WRONG method names like:
PrintWriter writer = response.getWriter(); writer.println("some text and HTML");
Use it for:
Printing text data to a character stream. Although you can still write character data to an OutputStream, this is the stream that’s designed to handle character data.
ServletOutputStream out = response.getOutputStream(); out.write(aByteArray);
Use it for:
Writing anything else!
FYI: The PrintWriter actually “wraps” the ServletOutputStream. In other words, the PrintWriter has a reference to the ServletOutputStream and delegates calls to it. There’s just ONE output stream back to the client, but the PrintWriter “decorates” the stream by adding higher-level character-friendly methods.