Java Reference
In-Depth Information
The writeStudentList() method takes a URLConnection object and a StudentList object as
parameters. This method first sets the appropriate URLConnection properties. This code is
listed as follows:
// Set this to false in order to ignore caching
// Set the content-type of the request
// application/octet-stream is used when writing
// application specific byte size data
// Set these vales to true to use the same connection
// for both input and output
These property settings are very important. The first executable line turns off caching for this
request. The second is one of the more important property settings. It specifies that you will be
using application-specific, byte-size data as your content type because you will be sending a
binary data stream. The last two lines in this section set the doInput and doOutput connection
properties to True . This makes it possible to both send and receive using the same connection.
The last section of the writeStudentList() method you will look at follows the same three
steps discussed in the section “Object Serialization.” It gets an OutputStream object from the
URLConnection.getOutputStream() method. This OutputStream object is then passed to the
constructor of the ObjectOutputStream , which is used to write the object to the HTTP connec-
tion. This code is listed as follows:
// Create the ObjectOutputStream passing it the
// OutputStream object.
ObjectOutputStream os =
new ObjectOutputStream(connection.getOutputStream());
// Write the StudentList to the ObjectOutputStream
System.err.println(“Writing StudentList Object.”);
The readStudentList() method is even more simple than its counterpart. It simply follows
the object serialization steps for reading an object. It gets an InputStream object from the
URLConnection and passes it to the constructor of the ObjectInputStream , which is then used
to read the StudentList object. For your convenience, it is listed in its entirety:
Search WWH ::

Custom Search