[Previous] [Contents] [Next] [IONA Technologies]


Chapter 3
Getting Started with Java Applets


Contents

3.1 Review of OrbixWeb Programming Steps
3.2 Providing a Server
3.3 Writing a Client Applet
3.3.1 Creating the User Interface
3.3.2 Adding OrbixWeb Client Functionality
3.3.3 Creating the Applet
3.3.4 Adding the Applet to a HTML File
3.3.5 Compiling the Client Applet
3.3.6 Running the Client Applet
3.4 Learning more about OrbixWeb



In Chapter 2, we introduced OrbixWeb by coding a simple example of a distributed system where both client and server were implemented as standalone Java applications. In this chapter, we will expand this example to take account of a common form of distributed Java systems: a downloadable client applet which communicates with a back-end server application.

The reader should be familiar with the material covered in Chapter 2, "Getting Started with Java Applications" before continuing with this chapter.

3.1 Review of OrbixWeb Programming Steps

Recall the programming steps typically required in creating a distributed client/server application using OrbixWeb:

In this chapter, we will illustrate our example using the same IDL interface for a two-dimensional grid as used in the previous chapter.

A version of the sample code described in this chapter is available in the gridApplet demonstration directory of your OrbixWeb installation. However, readers should be aware that the documented code may differ slightly from the installation example.

3.2 Providing a Server

In this chapter our aim is to illustrate a distributed architecture in which a downloadable client applet can communicate with an OrbixWeb server through an IDL interface definition. The server which we will use for this purpose is simply the server application developed in Chapter 2, "Getting Started with Java Applications".

This client-server architecture is a common requirement in the Java domain, where small, dynamic client applets may be downloaded to communicate with large, powerful back-end service applications. Architectures in which full OrbixWeb servers must be coded as downloadable applets are less common, and these will not be described here.

Of course, the client applet illustrated in this chapter provides very basic functionality and much more complex client-server interaction can be achieved within a similar architecture. Subsequent parts of this guide will expand upon the functionality described here.

3.3 Writing a Client Applet

We will now develop a simple Java applet which provides a graphical user interface to the IDL interface Grid. This example builds upon the concepts introduced in the OrbixWeb grid client application.

In this section, we will elaborate the following applet programming steps, relating each to a particular demonstration source file:

The package name for all the Java classes in this example is gridAppletDemo. Therefore, each of the above files is located in the gridAppletDemo subdirectory and we will assume that the IDL file grid.idl was compiled with the following command:

Note that development of an OrbixWeb client can be completely decoupled from the server-side development process, so the choice of package name when compiling the IDL file for the client may differ from the choice of package name for the server.

3.3.1 Creating the User Interface

The completed applet appears as follows:

The grid applet provides a very simple user interface. This interface is divided into two sections, each with a specific objective: a "Server Details" section designed to allow users to specify a target OrbixWeb server, and an "Object Details" section which allows a Grid object to be queried and updated.

The source code for GridPanel.java simply uses the Java Abstract Windowing Toolkit package (java.awt) to create and arrange each of the elements within a java.awt.Panel container. The details of how the interface is assembled are not relevant to this description, but we will reference the individual component names in the next section:

3.3.2 Adding OrbixWeb Client Functionality

In this example, all OrbixWeb client functions are initiated by button clicks in the user interface. For the purposes of illustration, the applet simply maps interface buttons directly to individual operations on a Grid object:

The text fields provide a means of obtaining and reporting operation parameter values and operation results.

To implement the functionality of the interface buttons, we add support for user interaction by detecting java.awt.Event.ACTION_EVENT events. There are several methods of achieving this, but in this example we define a subclass of GridPanel, called GridEvents, which overrides the java.awt.Component.action() method. This method allows us to receive notification of a button click event, and to determine which button in the interface received that event. We can then react by invoking the appropriate operation on a Grid proxy object.

Note that all sample code described below includes error handling as described in the section "Error Handling: Integration with Java Exceptions", but the method displayMsg() is introduced to display operation results. The functionality of this method will be explained in the next subsection.

We will now outline the source code for class GridEvents. The methods outlined here will be expanded later in this section.

In this sample code, a method is provided to handle the client functionality required by each button in the interface. The "Disconnect" button is the only exception, as it is implemented by a single line of Java code.

We will now explain in detail the functionality of each button implementation. The "Connect" button functionality is implemented in the method bindObject():

The "Connect" button forces the client to bind to a Grid object in the server specified by the "Server Name" and "Server Host" text fields. The _bind() method creates a proxy object of type Grid and binds it to an implementation object in the specified server. No object marker is specified in the _bind() call, so OrbixWeb is free to choose any Grid object in that server.

The "Disconnect" button is implemented by the following line of code:

This button simply allows the user to destroy a previously created proxy object by assigning it to the Java value null.

The "Get Grid Dimensions", "Get Cell Value" and "Set Cell Value" buttons allow Grid operations to be invoked on a proxy created by the "Connect" button. Therefore, the methods which implement these buttons simply attempt invocations on the proxy member variable gRef:

Error Handling: Integration with Java Exceptions

In the client example described in Chapter 2, "Getting Started with Java Applications", we handled OrbixWeb system exceptions in catch clauses by displaying the exception toString() output in the System.out print stream. Access to this string information is often useful and can facilitate debugging of OrbixWeb clients. In a client applet, it may not be practical to output the information to a print stream, so for this example we display exception strings in information dialog boxes. The file MsgDialog.java implements a generic dialog class for this purpose:

The details of this class implementation and that of class Msg (on which class MsgDialog relies) are not important. OrbixWeb error handling is now added to our GridEvents class by defining a display method:

This simply allows any string (including a system exception string) to be displayed in a dialog box.

3.3.3 Creating the Applet

To create the Grid client applet, we simply define a subclass of java.applet.Applet and add a GridEvents object to it:

3.3.4 Adding the Applet to a HTML File

With respect to HTML, an OrbixWeb applet client behaves exactly like a standard Java applet. It can therefore be included in a HTML file using the <APPLET> tag. The source for GridApplet.html is an example of this:

3.3.5 Compiling the Client Applet

The compilation instructions for an OrbixWeb applet are identical to those for a standard OrbixWeb application, as described in section 2.13.

In particular, it is necessary to ensure that the Java compiler can access the Java API packages (including java.awt for this sample code), the OrbixWeb IE.Iona.Orbix2 package, and any applet-specific classes. The compiler should then be invoked on all the Java source files for the application. In this example applet, the required files are Grid.java, _GridRef.java, GridPanel.java, GridEvents.java, GridApplet.java, MsgDialog.java, and Msg.java.

The OrbixWeb gridApplet demonstration directory provides a make file which invokes the javac compiler as required. In this directory, it is simply necessary to type (on Unix):

or (on Windows 95 or Windows NT):

3.3.6 Running the Client Applet

To run the client applet, it is necessary to use an applet viewer or Web browser to view the HTML file. For example, the JDK appletviewer can be used as follows:

Java applets differ slightly from standalone Java applications in the requirements for providing access to the relevant class directories.

If an OrbixWeb applet is loaded into the viewer from a file, then it is possible to specify the locations of required classes in the CLASSPATH environment variable before running the viewer. The classes required are identical to those for an OrbixWeb client application, as described in section 2.14.

An alternative approach, when loading the applet from a file, is to provide access (for example, via links) to all the classes the applet requires in a single directory. Then, rather than setting an environment variable, the CODEBASE attribute of the HTML flag <APPLET> could be used to indicate the location of the applet bytecode.

For example, this would involve changing the <APPLET> tag in GridApplet.html to the following:

When loading an applet from a Web server, the CLASSPATH environment variable approach to specifying the location of bytecode is not universally available. In this case, the only suitable strategy is to set the CODEBASE attribute as described above. Note that when using a Java-enabled Web browser to view an applet, it is not necessary to provide access to the Java API classes, as these will already be available.

Java applets are subject to important security restrictions, imposed by the Java environment and by web browsers. The severity of these restrictions is often dependent on browser technology, but the general issues are outlined in section 10.2.3 "Security Issues for Client Applets".

3.4 Learning more about OrbixWeb

The OrbixWeb Programming Guide describes the features of OrbixWeb in more detail–it expands on the information presented here and:

A number of features which extend the power of OrbixWeb are discussed, in particular:

A full description of the API to OrbixWeb is supplied in the OrbixWeb Reference Guide.



[Roadmap] [Introduction] [GS: Applications] [GS: Applets]
[IDL] [Mapping] [Programming OrbixWeb] [Publishing Objects] [Retrieving Objects] [IIOP]
[Running Clients] [Running Servers] [Exceptions] [Inheritance] [Callbacks] [Contexts]
[API Configuration] [TypeCode] [Any] [DII] [IR] [Filters] [Smart Proxies] [Loaders] [Locators]
[Index]