Input and Output Streams

 < Free Open Study > 

In the previous section we noted two stream classes for reading in the request and writing back the response:

  • ServletInputStream

  • ServletOutputStream

These classes are abstract classes that the servlet container is responsible for implementing. The ServletInputStream class extends from the abstract class. This is the base class for input streams of bytes, and extending from it allows us to construct any necessary input reader objects that take the InputStream class in the constructor. This allows us much greater flexibility in terms of the data that we can read in from the client. We can read any data that we can read from an InputStream. We can use the to read in Java serialized objects from a Java applet or application client. We could also use a to read from the input stream and buffer the input and to support the additional functionality this class provides.

Similarly the ServletOutputStream class extends from the abstract class. This is the base class for outputting streams of bytes. This allows us to send out response data back with any I/O class that wraps the OutputStream class and enhances its functionality. Therefore, among other things, we can serialize Java objects back to the client using the class, or use a PrintWriter to write text to the stream.

These streams provide servlets with the flexibility to support almost any type of client application, including browsers, Java applets, Java applications, and other applications. We use the streams to receive client data and to return our response back to the client. We rarely use the ServletInputStream and ServletOutputStream directly. Normally we wrap these objects in more suitable classes from the package.

The ServletInputStream Class

The primary use of the ServletInputStream object is to wrap in a suitable InputStream class from the package. It can be used wherever you would use an InputStream to construct an input class such as a DataInputStream for reading primitive types (and Strings), or an ObjectOutputStream for reading serialized Java objects.

For example, we could read in a number of Java objects as follows:

    ServletInputStream input = request.getInputStream();    ObjectInputStream ois = new ObjectInputStream(input);    String someEvent = (String) ois.readObject();    Date eventDate = (Date) ois.readObject();    input.close(); 

As you can see, the three steps involved are as follows:

  1. Access the request's InputStream and wrap it in an ObjectInputStream.

  2. Read in the objects. If we know the objects that are being received, we can cast them into their sub-type. Alternatively we could perform a check (using the instanceof operator) before casting. Obviously if we try to cast an object invalidly we will get a run time error.

  3. Finally we close the stream.

The ServletInputStream class defines the following method, which is used to read from the input stream, a line at a time:

    public readLine(byte[] b, int off, int len) 

The method returns -1 if the end of the stream is reached before the line has been read.

ServletOutputStream class

Similar to the ServletInputStream class, the primary use by servlet programmers for this class is to wrap it in a suitable OutputStream class from the package. It can be used wherever you would use an OutputStream to construct an output class.

For example, we could serialize to a Java client a number of Java objects, wrapping in the ObjectOutputStream class as follows:

    ServletOutputStream output = response.getOutputStream();    ObjectOutputStream oos = new ObjectOutputStream(output);    oos.writeObject("a String");    oos.writeObject(new Date());    output.close(); 

Here, the three steps involved are as follows:

  1. Access the response's OutputStream and wrap it in an ObjectOutputStream

  2. Write out the objects to the ObjectOutputStream

  3. Close the stream

The ServletOutputStream class provides 15 overloaded variants of the print() and println() methods.

The basic print() methods take the full range of primitive types (boolean, char, double, float, int, long) or a String object as parameters, and prints them to the stream. For example:

    public void print(boolean b) 

The following variant of the println() method writes a carriage return-line feed (CRLF) to the stream:

    public void println() 

The println() methods print the full range of primitive types (boolean, char, double, float, int, long) or a String object to the stream, followed by a carriage return-line feed (CRLF). For example:

    public void println(float f) 

We can also use the class for output. If we are returning character based (text) data to the client, we would normally use a PrintWriter object to write to the stream. The process is similar to that shown for outputting with the ServletOutputStream. The main methods used are the println() or print() methods, similar to those of the ServletOutputStream. These are overloaded to take any primitive type or a String as parameter (see the Javadoc for more information at: For example:

    PrintWriter out = response.getWriter();    out.println("<html><head><title>The Page</title></head>");    out.println("<body>Some text</body></html>");    out.close(); 

 < Free Open Study > 

Professional Java Servlets 2.3
Professional Java Servlets 2.3
ISBN: 186100561X
EAN: 2147483647
Year: 2006
Pages: 130 © 2008-2017.
If you may any questions please contact us: