Sending a Response to the Browser
Probably the biggest difference between Java Server Pages and servlets 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 the servlet, however, the response is usually in the form of codemostly 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. 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 3.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.
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().
Although the ServletOutputStream class also contains print and println methods such as 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 are done writing out the response; the servlet engine knows you are through when the service method has finished executing. In addition to giving you the output streams, the response object performs other interesting tasks. You can control the amount of buffering, flush the output stream, and even clear the output stream. The ServletResponse is discussed in detail in Chapter 13, "Core Servlet Components."
Make sure you set the content type before you call getWriter. Because the servlet engine modifies the character set mapping depending on the content type, it needs to know the content type before it creates the Writer.