5.2 Sending a Normal Response

Java Servlet Programming, 2nd Edition > 5. Sending HTML Information > 5.2 Sending a Normal Response

 
< BACKCONTINUE >

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.

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 {     res.setContentType("text/html");     PrintWriter out = res.getWriter();     out.println("<HTML>");     out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");     out.println("<BODY>");     out.println("<BIG>Hello World</BIG>");     out.println("</BODY></HTML>");   } }

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 13, 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; and this method 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 in order 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 a 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 and later, 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 datatypes (see Appendix A, for a complete list). The only difference between the ServletOutputStream interface and that of a PrintStream is the print( ) and println( ) methods of ServletOutputStream inexplicably cannot directly print parameters of type Object or char[].


Last updated on 3/20/2003
Java Servlet Programming, 2nd Edition, © 2001 O'Reilly

< BACKCONTINUE >


Java servlet programming
Java Servlet Programming (Java Series)
ISBN: 0596000405
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net