Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
442
Chapter 17 Using Applets As Servlet Front Ends
4. Set any desired HTTP headers. If you want to set HTTP
request headers (see Chapter 4), you can use
setRequest
Property
to do so.
connection.setRequestProperty("header", "value");
5. Create an
ObjectInputStream
. The constructor for this class
simply takes the raw input stream from the
URLConnection
. It
is at the point where you create the input stream that the con
nection to the Web server is actually established.
ObjectInputStream in =
new ObjectInputStream(connection.getInputStream());
6. Read the data structure with
readObject
. The return type
of
readObject
is
Object
, so you need to make a typecast to
whatever more specific type the server actually sent.
SomeClass value = (SomeClass)in.readObject();
doSomethingWith(value);
7. Close the input stream.
in.close();
The Server Side
A servlet needs to perform the following four steps to send serialized data
structures to an applet. Assume that
request
and
response
are the
Http
ServletRequest
and
HttpServletResponse
objects supplied to the
doGet
and
doPost
methods. Again, these steps are simplified slightly by the omis
sion of the required
try
/
catch
blocks.
1. Specify that binary content is being sent. This task is
accomplished by designating
application/x java serialized object
as the MIME type of the response. This is the standard MIME
type for objects encoded with an
ObjectOutputStream
,
although in practice, since the applet (not the browser) is read
ing the result, the MIME type is not very important. See the
discussion of
Content Type
in Section 7.2 (HTTP 1.1
Response Headers and Their Meaning) for more information
on MIME types.
String contentType =
"application/x java serialized object";
response.setContentType(contentType);
Home page for this book: www.coreservlets.com; Home page for sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved