PrintWriter Vs ServletOutputStream


Earlier we have seen the ways of receiving data from client. Now let us do with response. The response object functionality is to send data to client of what client is interested. For this, response uses IO Streams. There are two streams involved depending on the nature of data like text data or binary data. For text data, the response uses PrintWriter and for binary data it uses ServletOutputStream. Two methods exist in ServletResponse interface (inherited by HttpServletResponse) to return the objects of PrintWriter and ServletOutputStreamgetWriter() and getOuptputStream(). That is, there are two styles exist.

First let us see what Servlet API says about these methods defined in javax.servlet.ServletResponse interface.

  • PrintWriter getWriter()throws IOException: Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response’s character encoding has not been specified as described in getCharacterEncoding (i.e., the method just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.
  • ServletOutputStream getOutputStream() throws java.io.IOException: Returns a ServletOutputStream suitable for writing binary data in the response. The servlet container does not encode the binary data.
PrintWriter Vs ServletOutputStream

PrintWriter object to send textual data to client:

getWriter() method of ServletResponse (inherited by HttpServletResponse) returns an object of PrintWriter. This is the general way of sending data to client as most response represents text or HTML only.

  PrintWriter out = response.getWriter();
  out.println("VALID");                      // sending text data
  out.println("INVALID");             // sending HTML data 

This is shown in the first Servlet program Login Screen Validation.

ServletOutputStream object to send binary data to client:

Sometimes, the Servlet may need to send images and JAR files etc. to the client. They do not include text data. To send them to client, the Programmer should convert this data into binary format and to send this binary data to client, the ServletOutputStream is used.

It is an abstract class (the abstract methods are overridden by Servlet container). Methods available in this class are overloaded print() and println().

Following is the hierarchy

java.lang.Object –> java.io.OutputStream –> javax.servlet.ServletOutputStream

ServletOutputStream object is obtained with getOutputStream() method of ServletResponse (inherited by HttpServlerResponse) as follows.

  ServletOutputStream sos = resonse.getOutputStream();
  sos.write(barray);

barray is a byte array into which binary data is read. This style is used in four examples – Sending Image to Client, Read HTML File Contents, response.getOutputStream() and Multiple Submit Buttons in a single HTML Form.

Small Table showing the big difference of PrintWriter Vs ServletOutputStream.
Property PrintWriter ServletOutputStream
class nature Concrete class Abstract class
Method that returns getWriter() getOutputStream()
Nature of data To send character-based information like textual/HTML data Mostly used to send binary data and less times to send primitive values and character-based information
Character encoding Does character encoding for ASCII/Unicode characters (as per the getCharacterEncoding() method) Does not perform any character conversion as it is meant for binary data (container does not encode binary data).
Performance Very expensive for character conversion Faster as no character conversion exists and particularly the entire data is sent as a byte array at a time
Type of stream Character stream Byte stream
Usage Mostly used Limited usage

Pass your comments and suggestions on this tutorial "PrintWriter Vs ServletOutputStream".

1 thought on “PrintWriter Vs ServletOutputStream”

Leave a Comment

Your email address will not be published.