home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book Home Java Servlet Programming Search this book

5.2. Sending a Normal Response

Let's begin our discussion of servlet responses with another look at the first servlet in this book, the HelloWorld servlet, shown in Example 5-1. We hope it looks a lot simpler to you now than it did back in Chapter 2, "HTTP Servlet Basics".

Example 5-1. Hello again

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {

    PrintWriter out = res.getWriter();

    out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
    out.println("<BIG>Hello World</BIG>");

This servlet uses two methods and a class that have been only briefly mentioned before. The setContentType() method of ServletResponse sets the content type of the response to be the specified type:

public void ServletResponse.setContentType(String type)

In an HTTP servlet, this method sets the Content-Type HTTP header.

The getWriter() method returns a PrintWriter for writing character-based response data:

public PrintWriter ServletResponse.getWriter() throws IOException

The writer encodes the characters according to whatever charset is given in the content type. If no charset is specified, as is generally the case, the writer uses the ISO-8859-1 (Latin-1) encoding appropriate for Western European languages. Charsets are covered in depth in Chapter 12, "Internationalization", so for now just remember that it's good form to always set the content type before you get a PrintWriter. This method throws an IllegalStateException if getOutputStream() has already been called for this response; it throws an UnsupportedEncodingException if the encoding of the output stream is unsupported or unknown.

In addition to using a PrintWriter to return a response, a servlet can use a special subclass of java.io.OutputStream to write binary data--the ServletOutputStream, which is defined in javax.servlet. You can get a ServletOutputStream with getOutputStream():

public ServletOutputStream ServletResponse.getOutputStream() throws IOException

This method returns an ServletOutputStream for writing binary (byte-at-a-time) response data. No encoding is performed. This method throws an IllegalStateException if getWriter() has already been called for this response.

The ServletOutputStream class resembles the standard Java PrintStream class. In the Servlet API Version 1.0, this class was used for all servlet output, both textual and binary. In the Servlet API Version 2.0, however, it has been relegated to handling binary output only. As a direct subclass of OutputStream, it makes available the write(), flush(), and close() methods of the OutputStream class. To these it adds its own print() and println() methods for writing most of the primitive Java data types (see Appendix A, "Servlet API Quick Reference", for a complete list). The only difference between the ServletOutputStream interface and that of a PrintStream is that the print() and println() methods of ServletOutputStream inexplicably cannot directly print parameters of type Object or char[].

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.