O'Reilly logo

Java Servlet Programming by Jason Hunter

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

The DaytimeApplet user interface

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required