Use Pre-Encoded Characters
One of the first things you learn
when programming servlets is to use a
PrintWriter
for writing characters and an
OutputStream
for writing bytes. And while that’s stylistically
good advice, it’s also a bit simplistic.
Here’s the full truth: just because
you’re outputting characters
doesn’t mean you should always use a
PrintWriter
!
A PrintWriter
has a downside: specifically, it has
to encode every character from a
char
to a
byte
sequence
internally. When you have content that’s already
encoded—such as content in a file, URL, or database, or even in
a
String
held in memory—it’s often better to stick with
streams. That way you can enable a straight byte-to-byte transfer.
Except for those rare times when there’s a charset
mismatch between the stored encoding and the required encoding,
there’s no need to first decode the content into a
String
and then encode it again to bytes on the
way to the client. Use the pre-encoded characters and you can save a
lot of overhead.
To demonstrate, the servlet in Example 3-1 uses a reader to read from a text file and a
writer to output text to the client. Although this follows the mantra
of using
Reader
/Writer
classes for text, it involves a wasteful, needless conversion.
import java.io.*; import java.util.prefs.*; import javax.servlet.*; import javax.servlet.http.*; public class WastedConversions extends HttpServlet { // Random file, for demo purposes only String name = "content.txt"; ...
Get Java Enterprise Best Practices now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.