[Previous] [Table of Contents]

Bojangles Tutorial
Developing the applet

Now that the two Account objects exist on the server, and there is a servlet running an ORB available for providing access to those objects, a client is needed.

Bojangles implements the client as a Java applet. The Java applet runs a client side ORB allowing the applet to resolve references to two Account objects.

The complete source for the applet can be found in the AccountsApplet.java source file. The following is the relevant portions of the applet:

package COM.ibm.jaws.tutorial1;

import java.applet.Applet;
import java.io.*;
import java.net.*;
import java.awt.*;
import COM.ibm.jaws.mofw.BaseCollection;
import COM.ibm.jaws.mofw.NamedCollection;
import COM.ibm.jaws.services.filepobc.FilepoBCImpl;
import COM.ibm.jaws.services.bcfw.Syncable;
import COM.ibm.jaws.services.sroot.SimpleRootBC;
import COM.ibm.corba.ClientORBlet;
import COM.ibm.corba.CORBA;

public class AccountsApplet extends java.applet.Applet
{
    protected BaseCollection accountCollection = null;
    protected ClientORBlet orb = null;

    // References for the persistent, distributed, business objects.
    protected Account checkingAccount = null;
    protected Account savingsAccount = null;

    // User interface fields
              .
              .
              .

    public void
    init()
    {
	resolveAndActivateAccounts();
	buildUserInterface();
    }

    protected void
    resolveAndActivateAccounts()
    {
	orb = CORBA.ORB_init(this);
	
	accountCollection = 
	    (BaseCollection)
	    orb.URLToObject(getParameter("URLToObjectContext") +
			    ";/AccountCollection" +
			    "?COM.ibm.jaws.mofw.BaseCollection");

	// Activate the accounts using "resolve"
	checkingAccount =
	    (Account) accountCollection.resolve("Checking");
	savingsAccount =
	    (Account) accountCollection.resolve("Savings");
    }

    protected void
    buildUserInterface()
    {
        .
        .
        .
    }

    public boolean
    action(Event event,
	   Object object)
    {
	// Execute update if the update button was pressed.
	if (event.target instanceof Button &&
	    ((String) object).equals(kUpdateLabel)) {
	    updateButtonCallback();
	}

	return true;
    }
    
    public void
    destroy()
    {
        .
        .
        .
    }

    protected void
    updateButtonCallback()
    {
	try {
	    // Tell the user via the UI that we are working on the update.
	    statusTextArea.replaceText(kUpdateRequested, 0, 1000);
	    
	    float adjustedBalance = 0;

	    // Do the deposit/withdrawal on the appropriate account, then
	    // get the new account balance.
	    if (accountType.getSelectedItem() == kChecking) {

		if (updateType.getSelectedItem() == kWithdrawal) {
		    checkingAccount.withdrawal
			(Float.valueOf(amount.getText()).floatValue());
		}
		else {
		    checkingAccount.deposit
			(Float.valueOf(amount.getText()).floatValue());
		}

		// Get the new balance.
		adjustedBalance = checkingAccount.getBalance();
	    }
	    else {
		if (updateType.getSelectedItem() == kWithdrawal) {
		    savingsAccount.withdrawal
			(Float.valueOf(amount.getText()).floatValue());
		}
		else {

		    savingsAccount.deposit
			(Float.valueOf(amount.getText()).floatValue());
		}

		// Get the new balance.
		adjustedBalance = savingsAccount.getBalance();
	    }


	    // Update the user interface.
	    statusTextArea.replaceText("Adjusted balance: " + adjustedBalance,
				       0, 1000);
	    statusTextArea.appendText("\n\n" + kUpdateInstructions);

	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}

The most important method of the applet is the resolveAndActivateAccounts. This method first initializes the Bojangles client side ORB via:

	orb = CORBA.ORB_init(this);

The next line resolves the FilepoBC database object found in the activation space of the AccountsServlet:

	accountCollection = 
	    (BaseCollection)
	    orb.URLToObject(getParameter("URLToObjectContext") +
			    ";/AccountCollection" +
			    "?COM.ibm.jaws.mofw.BaseCollection");

The URLToObjectContext applet parameter is specified in the index.html file as /server-java/AccountsServlet.

Once the FilepoBC database has been resolved, use the resolve method to obtain references to the checking and savings objects:

	// Activate the accounts using "resolve"
	checkingAccount =
	    (Account) accountCollection.resolve("Checking");
	savingsAccount =
	    (Account) accountCollection.resolve("Savings");

At this point, the deposit, withdrawal, and getBalance methods calls can be called on both the checking and savings account objects. These methods will result in updating the versions of the objects stored on the Netscape server in the FilepoBC database.

In order to execute the AccountsApplet, an html file needs to be created. The HTML source for the AccountsApplet is located in index.html.


[Previous] [Table of Contents]