The Client Code Comparison

Gopalan Suresh Raj

Implementing the MTS/EJB Client

Both the EJB and MTS clients have been implemented using Java code using the Visual J++ version 6.0 compiler.

MTS Client - The MTS client shown below calls into the MTS server object's methods by first acquiring a pointer to the server object. The new keyword here instantiates the bank.Checking MTS Server object. This leads the Microsoft JVM to use the CLSID to make a CoCreateInstance() call. The IUnknown pointer returned by CoCreateInstance() is then cast to bank.IChecking, as shown below.

EJB Client - The EJB client shown below calls into the EJB server object's methods by first acquiring a pointer to the server object. The client first uses JNDI to get a reference to the EJB component's home interface. It then uses this reference to create an instance of the EJB component and holds on to this reference when calling into the component methods.

MTS - Client Code

EJB - Client Code
import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;
import com.ms.com.*;

import bank.*;

import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;
import com.ms.com.*;

import java.rmi.*;
import java.util.*;
import Bank.*;
public class CheckingClient extends Form {
 
 static final int     INITIAL_RECORD = 11;
 
 int                     accountNo;
 String                name;
 double               balance;
 int                     count;

 bank.IChecking   server;
public class CheckingClient extends Form {
 
 static final int INITIAL_RECORD = 11;
 
 int       accountNo;
 String  name;
 double balance;
 int       count;
 
 public CheckingHome home = null;
 public Checking server = null;

  ////////////////////////////////////////////////////////////////
 public CheckingClient () {
 ////////////////////////////////////////////////////////////////
 
  super();
  initForm();
   try {
   server = (bank.IChecking) new bank.Checking ();
   }
   catch (Exception ex) {
    ex.printStackTrace ();
   }
  fillAccountList ();
 }
 ////////////////////////////////////////////////////////////////
 public CheckingClient () {
 ////////////////////////////////////////////////////////////////
 
  super();
  initForm();
  try {
  home = (CheckingHome) Naming.lookup ("checking");
  if (home == null) {
   System.out.println ("null TellerHome returned...");
  }
  server = home.create ();
  }
  catch (Exception e) {
   e.printStackTrace ();
  }
 fillAccountList ();
 }
 /**
  * CheckingClient overrides dispose so it can clean up the
  * component list.
  */

  ////////////////////////////////////////////////////////////////
 public void dispose() {
  ////////////////////////////////////////////////////////////////
  super.dispose();
  components.dispose();
 }
/**
  * CheckingClient overrides dispose so it can clean up the
  * component list.
  */

 ////////////////////////////////////////////////////////////////
 public void dispose() {
 ////////////////////////////////////////////////////////////////
  super.dispose();
  components.dispose();
 }
  ////////////////////////////////////////////////////////////////
 public void fillAccountList() {
  ////////////////////////////////////////////////////////////////
  accountList.removeAll ();
  count = 0;
  boolean bMoreElements = true;
  do {
   try {
    name = server.getCustomerName (count+INITIAL_RECORD);
    if(name == null)
     break;
 
    accountList.addItem ((new Integer (count+INITIAL_RECORD))
                                                                                  .toString ());
 
   }
   catch (Exception ex) {
    ex.printStackTrace ();
    bMoreElements = false;
   }
   ++count;
  } while (bMoreElements == true);
 }
 ////////////////////////////////////////////////////////////////
 public void fillAccountList() {
 ////////////////////////////////////////////////////////////////
  accountList.removeAll ();
  count = 0;
  boolean bMoreElements = true;
  do {
   try {
    name = server.getCustomerName (count+INITIAL_RECORD);
    if(name == null)
     break;
 
    accountList.addItem((new Integer (count+INITIAL_RECORD))
                                                                                  .toString ());
 
   }
   catch (Exception ex) {
    ex.printStackTrace ();
    bMoreElements = false;
   }
   ++count;
  } while (bMoreElements == true);

 }
  ////////////////////////////////////////////////////////////////
 private void retrieveButton_click(Object source, Event e) {
  ////////////////////////////////////////////////////////////////
 
  if (accountList.getSelectedItem () == null)
   return;
 
  String accSel = (accountList.getSelectedItem ()).toString ();
  accountNo = (new Integer (accSel)).intValue ();
 
  try {
 
   balance = server.getBalance (accountNo);
   name = server.getCustomerName (accountNo);
 

   numberEdit.setText ( (new Integer (accountNo)).toString () );
   nameEdit.setText (name);
   balanceEdit.setText ( (new Double (balance)).toString () );
 
  }
  catch (Exception ex) {
   ex.printStackTrace ();
  }
 }
 ////////////////////////////////////////////////////////////////
 private void retrieveButton_click(Object source, Event e) {
 ////////////////////////////////////////////////////////////////
 
if (accountList.getSelectedItem () == null)
   return;
 
  String accSel = (accountList.getSelectedItem ()).toString ();
  accountNo = (new Integer (accSel)).intValue ();
 
  try {

   balance = server.getBalance (accountNo);
  name = server.getCustomerName (accountNo);

 
   numberEdit.setText ( (new Integer (accountNo)).toString () );
   nameEdit.setText (name);
   balanceEdit.setText ( (new Double (balance)).toString () );
 
  }
  catch (Exception ex) {
   ex.printStackTrace ();
  }
 }
  ////////////////////////////////////////////////////////////////
private void addButton_click(Object source, Event e) {
  ////////////////////////////////////////////////////////////////
 
  accountNo = count+INITIAL_RECORD;
  name = nameEdit.getText ();
  balance = ( new Double (balanceEdit.getText ()) ).doubleValue ();
 
  if ((accountNo == 0) || (name == null))
   return;
 
  try {
   server.createAccount (accountNo, name, balance);
  }
  catch (Exception ex) {
   ex.printStackTrace ();
  }
 
  fillAccountList ();
 }
 ////////////////////////////////////////////////////////////////
private void addButton_click(Object source, Event e) {
 ////////////////////////////////////////////////////////////////
 
  accountNo = count+INITIAL_RECORD;
  name = nameEdit.getText ();
  balance = ( new Double (balanceEdit.getText ()) ).doubleValue ();
 
  if ((accountNo == 0) || (name == null))
   return;
 
  try {
   server.createAccount (accountNo, name, balance);
  }
  catch (Exception ex) {
   ex.printStackTrace ();
  }
 
  fillAccountList ();
 }
  ////////////////////////////////////////////////////////////////
private void closeButton_click(Object source, Event e) {
  ////////////////////////////////////////////////////////////////
  Application.exit ();
 }
 ////////////////////////////////////////////////////////////////
private void closeButton_click(Object source, Event e) {
 ////////////////////////////////////////////////////////////////
  Application.exit ();
 }
  ////////////////////////////////////////////////////////////////
 private void creditButton_click(Object source, Event e) {
  ////////////////////////////////////////////////////////////////
 
  if (accountList.getSelectedItem () == null)
   return;
 
  String accSel = (accountList.getSelectedItem ()).toString ();
  accountNo = (new Integer (accSel)).intValue ();
 
  double amount = ( new Double (creditEdit.getText ()) ).doubleValue ();
 
  try {
   server.credit (amount, accountNo);
   balance = server.getBalance (accountNo);
   name = server.getCustomerName (accountNo);

 
   numberEdit.setText ( (new Integer (accountNo)).toString () );
   nameEdit.setText (name);
   balanceEdit.setText ( (new Double (balance)).toString () );
 
  }
  catch (Exception ex) {
   ex.printStackTrace ();
  }
 }
 ////////////////////////////////////////////////////////////////
 private void creditButton_click(Object source, Event e) {
 ////////////////////////////////////////////////////////////////
 
  if (accountList.getSelectedItem () == null)
   return;
 
  String accSel = (accountList.getSelectedItem ()).toString ();
  accountNo = (new Integer (accSel)).intValue ();
 
  double amount = ( new Double (creditEdit.getText ()) ).doubleValue ();
 
  try {
   server.credit (amount, accountNo);
   balance = server.getBalance (accountNo);
   name = server.getCustomerName (accountNo);

 
   numberEdit.setText ( (new Integer (accountNo)).toString () );
   nameEdit.setText (name);
   balanceEdit.setText ( (new Double (balance)).toString () );
 
  }
  catch (Exception ex) {
   ex.printStackTrace ();
  }
 }
  ////////////////////////////////////////////////////////////////
 private void debitButton_click(Object source, Event e) {
  ////////////////////////////////////////////////////////////////
 
  if (accountList.getSelectedItem () == null)
   return;
 
  String accSel = (accountList.getSelectedItem ()).toString ();
  accountNo = (new Integer (accSel)).intValue ();
 
  double amount = ( new Double (debitEdit.getText ()) ).doubleValue ();
 
  try {
   server.debit (amount, accountNo);
   balance = server.getBalance (accountNo);
   name = server.getCustomerName (accountNo);

 
   numberEdit.setText ( (new Integer (accountNo)).toString () );
   nameEdit.setText (name);
   balanceEdit.setText ( (new Double (balance)).toString () );
 
  }
  catch (Exception ex) {
   ex.printStackTrace ();
  }
 }
 ////////////////////////////////////////////////////////////////
 private void debitButton_click(Object source, Event e) {
 ////////////////////////////////////////////////////////////////
 
  if (accountList.getSelectedItem () == null)
   return;
 
  String accSel = (accountList.getSelectedItem ()).toString ();
  accountNo = (new Integer (accSel)).intValue ();
 
  double amount = ( new Double (debitEdit.getText ()) ).doubleValue ();
 
  try {
   server.debit (amount, accountNo);
   balance = server.getBalance (accountNo);
   name = server.getCustomerName (accountNo);

 
   numberEdit.setText ( (new Integer (accountNo)).toString () );
   nameEdit.setText (name);
   balanceEdit.setText ( (new Double (balance)).toString () );
 
  }
  catch (Exception ex) {
   ex.printStackTrace ();
  }
 }
 /**
  * NOTE: The following code is required by the Visual J++ form
  * designer.  It can be modified using the form editor.  Do not
  * modify it using the code editor.
  */

 Container components = new Container();
 ListBox accountList = new ListBox();
 Button retrieveButton = new Button();
 Button closeButton = new Button();
 Button addButton = new Button();
 Edit numberEdit = new Edit();
 Edit nameEdit = new Edit();
 Edit balanceEdit = new Edit();
 Label label1 = new Label();
 Label label2 = new Label();
 Label label3 = new Label();
 Edit creditEdit = new Edit();
 Edit debitEdit = new Edit();
 Button creditButton = new Button();
 Button debitButton = new Button();

 private void initForm() {
  this.setText("CheckingClient");
  this.setAutoScaleBaseSize(new Point(5, 13));
  this.setClientSize(new Point(300, 336));

  accountList.setAllowDrop(true);
  accountList.setLocation(new Point(16, 216));
  accountList.setSize(new Point(128, 108));
  accountList.setTabIndex(0);
  accountList.setText("listBox1");
  accountList.setUseTabStops(true);

  retrieveButton.setLocation(new Point(184, 240));
  retrieveButton.setSize(new Point(96, 24));
  retrieveButton.setTabIndex(5);
  retrieveButton.setText("Retrieve");
  retrieveButton.addOnClick(new EventHandler(this.retrieveButton_click));

  closeButton.setLocation(new Point(184, 280));
  closeButton.setSize(new Point(96, 24));
  closeButton.setTabIndex(4);
  closeButton.setText("Close");
  closeButton.addOnClick(new EventHandler(this.closeButton_click));

  addButton.setLocation(new Point(96, 112));
  addButton.setSize(new Point(96, 24));
  addButton.setTabIndex(3);
  addButton.setText("Add Record");
  addButton.addOnClick(new EventHandler(this.addButton_click));

  numberEdit.setLocation(new Point(120, 16));
  numberEdit.setSize(new Point(160, 20));
  numberEdit.setTabIndex(10);
  numberEdit.setText("-1");
  numberEdit.setReadOnly(true);

  nameEdit.setLocation(new Point(120, 48));
  nameEdit.setSize(new Point(160, 20));
  nameEdit.setTabIndex(9);
  nameEdit.setText("Nobody");

  balanceEdit.setLocation(new Point(120, 80));
  balanceEdit.setSize(new Point(160, 20));
  balanceEdit.setTabIndex(7);
  balanceEdit.setText("-1.0");

  label1.setLocation(new Point(16, 16));
  label1.setSize(new Point(88, 16));
  label1.setTabIndex(13);
  label1.setTabStop(false);
  label1.setText("Account Number");

  label2.setLocation(new Point(16, 48));
  label2.setSize(new Point(88, 16));
  label2.setTabIndex(12);
  label2.setTabStop(false);
  label2.setText("Customer Name");

  label3.setLocation(new Point(16, 80));
  label3.setSize(new Point(88, 16));
  label3.setTabIndex(11);
  label3.setTabStop(false);
  label3.setText("Balance (in US $)");

  creditEdit.setLocation(new Point(16, 152));
  creditEdit.setSize(new Point(128, 20));
  creditEdit.setTabIndex(8);
  creditEdit.setText("0");

  debitEdit.setLocation(new Point(16, 184));
  debitEdit.setSize(new Point(128, 20));
  debitEdit.setTabIndex(6);
  debitEdit.setText("0");

  creditButton.setLocation(new Point(184, 152));
  creditButton.setSize(new Point(96, 24));
  creditButton.setTabIndex(2);
  creditButton.setText("Credit (in US $)");
  creditButton.addOnClick(new EventHandler(this.creditButton_click));

  debitButton.setLocation(new Point(184, 184));
  debitButton.setSize(new Point(96, 24));
  debitButton.setTabIndex(1);
  debitButton.setText("Debit (in US $)");
  debitButton.addOnClick(new EventHandler(this.debitButton_click));

  this.setNewControls(new Control[] {
                      debitEdit,
                      debitButton,
                      creditButton,
                      creditEdit,
                      label3,
                      label2,
                      label1,
                      balanceEdit,
                      nameEdit,
                      numberEdit,
                      addButton,
                      closeButton,
                      retrieveButton,
                      accountList});
 }

/**
  * NOTE: The following code is required by the Visual J++ form
  * designer.  It can be modified using the form editor.  Do not
  * modify it using the code editor.
  */

 Container components = new Container();
 ListBox accountList = new ListBox();
 Button retrieveButton = new Button();
 Button closeButton = new Button();
 Button addButton = new Button();
 Edit numberEdit = new Edit();
 Edit nameEdit = new Edit();
 Edit balanceEdit = new Edit();
 Label label1 = new Label();
 Label label2 = new Label();
 Label label3 = new Label();
 Edit creditEdit = new Edit();
 Edit debitEdit = new Edit();
 Button creditButton = new Button();
 Button debitButton = new Button();

 private void initForm() {
  this.setText("CheckingClient");
  this.setAutoScaleBaseSize(new Point(5, 13));
  this.setClientSize(new Point(300, 336));

  accountList.setAllowDrop(true);
  accountList.setLocation(new Point(16, 216));
  accountList.setSize(new Point(128, 108));
  accountList.setTabIndex(0);
  accountList.setText("listBox1");
  accountList.setUseTabStops(true);

  retrieveButton.setLocation(new Point(184, 240));
  retrieveButton.setSize(new Point(96, 24));
  retrieveButton.setTabIndex(5);
  retrieveButton.setText("Retrieve");
  retrieveButton.addOnClick(new EventHandler(this.retrieveButton_click));

  closeButton.setLocation(new Point(184, 280));
  closeButton.setSize(new Point(96, 24));
  closeButton.setTabIndex(4);
  closeButton.setText("Close");
  closeButton.addOnClick(new EventHandler(this.closeButton_click));

  addButton.setLocation(new Point(96, 112));
  addButton.setSize(new Point(96, 24));
  addButton.setTabIndex(3);
  addButton.setText("Add Record");
  addButton.addOnClick(new EventHandler(this.addButton_click));

  numberEdit.setLocation(new Point(120, 16));
  numberEdit.setSize(new Point(160, 20));
  numberEdit.setTabIndex(10);
  numberEdit.setText("-1");
  numberEdit.setReadOnly(true);

  nameEdit.setLocation(new Point(120, 48));
  nameEdit.setSize(new Point(160, 20));
  nameEdit.setTabIndex(9);
  nameEdit.setText("Nobody");

  balanceEdit.setLocation(new Point(120, 80));
  balanceEdit.setSize(new Point(160, 20));
  balanceEdit.setTabIndex(7);
  balanceEdit.setText("-1.0");

  label1.setLocation(new Point(16, 16));
  label1.setSize(new Point(88, 16));
  label1.setTabIndex(13);
  label1.setTabStop(false);
  label1.setText("Account Number");

  label2.setLocation(new Point(16, 48));
  label2.setSize(new Point(88, 16));
  label2.setTabIndex(12);
  label2.setTabStop(false);
  label2.setText("Customer Name");

  label3.setLocation(new Point(16, 80));
  label3.setSize(new Point(88, 16));
  label3.setTabIndex(11);
  label3.setTabStop(false);
  label3.setText("Balance (in US $)");

  creditEdit.setLocation(new Point(16, 152));
  creditEdit.setSize(new Point(128, 20));
  creditEdit.setTabIndex(8);
  creditEdit.setText("0");

  debitEdit.setLocation(new Point(16, 184));
  debitEdit.setSize(new Point(128, 20));
  debitEdit.setTabIndex(6);
  debitEdit.setText("0");

  creditButton.setLocation(new Point(184, 152));
  creditButton.setSize(new Point(96, 24));
  creditButton.setTabIndex(2);
  creditButton.setText("Credit (in US $)");
  creditButton.addOnClick(new EventHandler(this.creditButton_click));

  debitButton.setLocation(new Point(184, 184));
  debitButton.setSize(new Point(96, 24));
  debitButton.setTabIndex(1);
  debitButton.setText("Debit (in US $)");
  debitButton.addOnClick(new EventHandler(this.debitButton_click));

  this.setNewControls(new Control[] {
                      debitEdit,
                      debitButton,
                      creditButton,
                      creditEdit,
                      label3,
                      label2,
                      label1,
                      balanceEdit,
                      nameEdit,
                      numberEdit,
                      addButton,
                      closeButton,
                      retrieveButton,
                      accountList});
 }

  /**
  * The main entry point for the application.
  *
  * @param args Array of parameters passed to the application
  * via the command line.
  */

 ////////////////////////////////////////////////////////////////
public static void main (String args[]) {
  ////////////////////////////////////////////////////////////////
  Application.run (new CheckingClient ());
 }
}
/**
  * The main entry point for the application.
  *
  * @param args Array of parameters passed to the application
  * via the command line.
  */

 ////////////////////////////////////////////////////////////////
public static void main (String args[]) {
 ////////////////////////////////////////////////////////////////
  Application.run (new CheckingClient ());
 }
}

CheckingClient.java

CheckingClient.java

Back to EJB vs MTS

click here to go to
My Advanced Java / J2EE Tutorial HomePage...

click here to go to
My COM+ / DNA Tutorial HomePage...

click here to go to
My CORBA Tutorial HomePage...


Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since May 04,1999.

Last Updated : May 04,'99

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-2000, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.