PrintWriter and ServletOutputStream
PrintWriter and ServletOutputStream are two distinct ways to write content to the response in a servlet environment in Java. They differ in their purpose, capabilities, and how they handle data.
PrintWriter
- Purpose: PrintWriter is used to write character-oriented data to the response, typically for sending textual content such as HTML, XML, or plain text.
- Character Encoding: PrintWriter handles character encoding automatically, ensuring proper conversion of characters to bytes based on the response’s encoding settings.
- Usage: It is suitable for writing textual data directly to the response stream, providing convenience methods for formatting and printing text.
- Example Use Cases: Sending HTML pages, JSON responses, or any text-based content.
ServletOutputStream
- Purpose: ServletOutputStream is used to write binary data to the response, such as images, PDF files, or any content that is not character-oriented.
- Byte Handling: ServletOutputStream deals directly with bytes, making it suitable for transmitting raw binary data without any character encoding conversions.
- Usage: It is commonly used when the servlet needs to send non-textual data, ensuring that bytes are transmitted without any character encoding transformations.
- Example Use Cases: Transmitting image files, downloading PDF documents, or streaming audio/video content.
Table of Contents
Differences
1. Data Handling
- Â PrintWriter: Handles character data and performs character encoding conversions.
- Â ServletOutputStream: Handles binary data directly as bytes, without character encoding conversions.
2. Use Cases
- Â PrintWriter: Suitable for writing textual content such as HTML, JSON, or plain text.
- Â ServletOutputStream: Suitable for transmitting binary content like images, PDFs, or any non-textual data.
3. Encoding:
- PrintWriter: Automatically manages character encoding based on the servlet’s response settings.
- ServletOutputStream: Deals with raw bytes and does not perform character encoding conversions.
Java Example
Let’s demonstrate the usage of PrintWriter and ServletOutputStream in a servlet to output textual and binary data respectively.
Servlet Code with PrintWriter:
Servlet Code with PrintWriter:
java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/textOutput")
public class TextOutputServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h2>Hello, PrintWriter!</h2>");
out.println("<p>This is a text-based response.</p>");
out.println("</body></html>");
}
}
Servlet Code with ServletOutputStream:
java
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/imageOutput")
public class ImageOutputServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("image/jpeg");
ServletOutputStream out = response.getOutputStream();
// Example: Read image from resources
InputStream inputStream = getServletContext().getResourceAsStream("/WEB-INF/images/sample.jpg");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
inputStream.close();
out.close();
}
}
Explanation of the PrintWriter and ServletOutputStream
- TextOutputServlet (/textOutput):
- Uses PrintWriter to write HTML content to the response. The servlet generates a simple HTML page with a greeting message.
- ImageOutputServlet (/imageOutput):
- Uses ServletOutputStream to transmit binary image data (image/jpeg) to the client. It reads the image file (sample.jpg) from the servlet context and streams its bytes directly to the response output stream.