[ Team LiB ] Previous Section Next Section

Sending a Response to the Browser

Probably the biggest difference between a JSP and a servlet is in the way responses are sent back to the browser. In a JSP, most of the response is embedded in the JSP in the form of static text in a template. In the servlet, however, the response is usually in the form of code—mostly calls to out.print and out.println.

A minimal servlet needs to do two things to send a response: Set the content type and write the response. The ServletRequest and ServletResponse objects help out in these areas. References to these objects are provided to the servlet as parameters of the service method.

If you are new to Web programming, you are probably unfamiliar with the notion of content type. Even though every request you make to a Web server involves a content type, it has probably been invisible to you. Whenever a browser asks the server for a file, the server sends back a content type along with the file. The content type tells the Web browser how it should display the file. For instance, if the file is an HTML file, the content type is text/html, whereas a JPEG image file has a content type of image/jpeg.

Most Web servers determine the content type by looking at the extension on the filename (for example, .htm and .html indicate HTML files, whereas .jpg indicates a JPEG image). In the case of a servlet, however, the Web server can't guess what the servlet is going to send back. It could be sending back HTML, XML, WML, or even a JPEG image! Instead, the Web server relies on the servlet to tell it what is being returned.

As you saw earlier in Listing 2.1, you set the content type by calling the setContentType method in the ServletResponse object. If you already know the exact length of the response, you can also call the setContentLength method. If you are just sending an HTML response, you don't need to send the content length. The browser can figure it out.

Set the Content Type Before Getting a Writer


Make sure you set the content type before you call getWriter. Because the servlet container depends on the content type when it modifies the character set mapping, it needs to know the content type before it creates the Writer.

After you have set the content type, you are ready to send the response. If you are sending a text response, as is the case with HTML and XML, you should use the PrintWriter object returned by response.getWriter(). If you are sending a binary file, such as a JPEG image, an audio file, or an animation file, you should use the ServletOutputStream returned by response.getOutputStream().

Use PrintWriter with Character Data


Although the ServletOutputStream class also contains print and println methods like the PrintWriter class, you should use the PrintWriter object when sending text output. Some content types require a slightly different character set, and the PrintWriter object automatically adjusts the character set based on the content type. In fact, the general rule of thumb with Java I/O is that you should always use a Writer object when writing character data.

You don't need to worry about closing the output stream when you finish writing out the response; the servlet container knows you are through when the service method has finished executing. In addition to giving you the output streams, the ServletResponse object performs other interesting tasks. You can control the amount of buffering, flush the output stream, and even clear the output stream.

ServletResponse is discussed in detail in Hour 6, "Looking under the Hood—Core Servlet Components."

    [ Team LiB ] Previous Section Next Section