Daytime Server
For a simple demonstration of each communication technique, we’re going to write an applet that asks its server for the current time of day. The applet first uses an HTTP connection, then a non-HTTP socket connection, and finally an RMI connection. Of course, an applet can normally get the current time from the system on which it’s running. To give this example an air of practicality, let’s assume the applet needs an approximate time stamp for some event and cannot rely on the client machine to have a correctly set clock.
The Applet
We’re going to be using the same example applet throughout this
section. The skeleton code for this applet,
DaytimeApplet
, is shown in Example 10.1. Right now, the applet just creates a user
interface where the times it retrieves can be displayed, as shown in
Figure 10.1. As we proceed with this example,
we’ll implement its getDateUsingHttpText()
,
getDateUsingHttpObject()
,
getDateUsingSocketText()
,
getDateUsingSocketObject()
, and
getDateUsingRMIObject()
methods. Note that the
examples in this chapter use several JDK 1.0 methods that are
deprecated in JDK 1.1. This is to maximize portability.
Figure 10-1. The DaytimeApplet user interface
Example 10-1. DaytimeApplet, without all the good stuff
import java.applet.*; import java.awt.*; import java.io.*; import java.util.*; public class DaytimeApplet extends Applet { TextField httpText, httpObject, ...
Get Java Servlet Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.