Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Bob Swart (aka Dr.Bob) - Medical Officer TDMWeb Kylix Developer's Guide
 Dr.Bob Examines... #27
See Also: other Dr.Bob Examines columns or JBuilder articles

Consuming Web Services using JBuilder 6 Enterprise
and the Web Services Kit for Java 1

In this article, we'll use Borland JBuilder 6 Enterprise together with the Borland Web Services Kit for Java to consume the Euro Web Service (originally written using Borland Delphi 6 and Kylix 2).

Euro Web Service
I assume you know the Euro web service that I implemented in Delphi 6 Enterprise as well as Kylix 2 Enterprise. This Web Service consists of two methods: FromEuro and ToEuro, both with two arguments: Currency and Amount. The first argument, Currency, is a 3-character string and defines the currency the web service will convert from or to Euros. At this time, 12 countries are ready to start using the Euro as of 1-1-2002, so we have twelve currencies: "FIM", "ITL", "NLG", "ESP", "BEF", "ATS", "LUF", "DEM", "FRF", "IEP", "PTE", and "GRO". The second argument, Amount, is a double that contains the amount of money we want to convert from or to Euros.
The Web Service itself is compiled using Delphi 6 and Kylix 2, and hence deployed on the internet in two different places (hosted by TDMWeb): a Windows 2000 web server using http://www.eBob42.com/cgi-bin/Euro42.exe/wsdl/IEuro or a Linux web server using http://www.drbob42.co.uk/cgi-bin/Euro42/wsdl/IEuro.
We'll be using the Delphi 6 version in this article (for no particular reason - the Kylix 2 version is exactly the same).

Figure 1.
WSDL for the Euro Web Service in Internet Explorer

JBuilder 6 Enterprise
Now that the preparations are finished, let's start JBuilder 6 Enterprise. I assume you have also installed the Borland Web Services Kit for Java (available for download here), otherwise you'll find that you can't easily work with the WSDL file.
After you've started JBuilder 6, do File | New Project to start a new project. Specify EuroClient as the Name of the new project and click on Finish to create the new EuroClient project.
The first thing you need to do now is to start a new application - the web service client (also called consumer). So do File | New and select Application from the Object Gallery. On the first page of the Application Wizard dialog that follows, you can specify the Package (euroclient) and Class (Application1) for your application (just leave the defaults) and on the next page you can specify some frame class options, such as the Class (Frame1) and Title (I've changed the title from "Frame Title" to "Euro Calculator"). Finally, click on Finish to create the new application and frame.

Importing WSDL
So far so good, but we've only made the skeleton application. We now need to add the IEuro wsdl definition to our project. We can do this in two ways: using an existing WSDL file or using a dynamic WSDL URL.
For the WSDL file, we first have to save the WSDL content we saw in Figure 1, put it inside Euro.wsdl, add it to our project (right-click on the project node and select "Add Files / Packages") and then generate Java classes from it, or we can start the Import WSDL Wizard which can be found in the WebServices tab of the Object Gallery:

Figure 2.
WSDL to Java Source Generator

The Import WSDL Wizard is also the tool that we need to use for a dynamic WSDL URL, so let's use this new tool.
As you can see in Figure 3, we only need to specify the WSDL URL. By default, this is a URL of the form http://www.eBob42.com/cgi-bin/Euro42.exe/wsdl/IEuro (for the Web Service written in Delphi 6). In case you want to use an existing WSDL file, you can click on the ellipsis and locate the file on your harddisk.

Figure 3.
WSDL to Java Generator Wizard

Note that we don't need to generate a skeleton (which would be used to create a Web Service "server"), but only the stub, because we are consuming an existing web service.
After you click on Finish, there will be three new files added to your EuroClient project, placed in the com.borland.www package, namely IEuro.java, IEurobindingStub.java and IEuroservice.java (all three generated from the definitions found in the WSDL file).

Figure 4.
JBuilder 6 with IEuro WSDL imported

The generate source file IEuro.java contains the definition of the interface IEuro, which is derived from java.rmi.Remote - meaning that we have a remote invokable interface here - and contains two methods: FromEuro and ToEuro.
  /**
   * IEuro.java
   *
   * This file was auto-generated from WSDL
   * by the Apache Axis Wsdl2java emitter.
   */

  package com.borland.www;

  public interface IEuro extends java.rmi.Remote {
      public double FromEuro(java.lang.String Currency, double Amount)
        throws java.rmi.RemoteException;
      public double ToEuro(java.lang.String Currency, double Amount)
        throws java.rmi.RemoteException;
  }
The generated source file IEurobindingStub.java contains the stub for the IEuro binding, meaning the actual class that we can use in our Web Service consumer application, and that will act as the (remote) web service itself, but in fact only consists of a stub that will invoke the remote interface.

Euro Conversion
Now that the pieces are in place it's time to start using the Web Service. Go back to the frame, click on the Designer tab, and start dropping some Swing components on it.
Personally, I always start by changing the Layout property of the contentPane of the Frame to XTLayout. After this, I find it easier to drop and position controls. For this example, we need only five controls:

Set the name property of the first jTextField to jTextFieldCurr, and set the name property of the second jTextField to jTextFieldEuro. Set the text property of the jTextFields to "0.0" and you may also want to set their preferredSize property to 72,21 in order to make sure they're wide enough to show the converted results.
Set the name property of the jComboBox to jComboBoxCurr. In order to fill the jComboBoxCurr with the list of twelve possible currencies that can be converted to Euros, we should change the declaration of jComboBoxCurr in the Frame1 constructor as follows:
  String[] EuroCurr =
    {"FIM", "ITL", "NLG", "ESP", "BEF", "ATS", "LUF", "DEM", "FRF", "IEP", "PTE", "GRO"};
  JComboBox jComboBoxCurr = new JComboBox(EuroCurr);
And since I'm from The Netherlands, I've also changed the selectedIndex property of jComboBoxCurr to 2, so the default choice of the combobox will show NLG (but feel free to specify your own default choice here).
Finally, set the name property of the first jButtons to jButtonFromEuro and set the name property of the second jButton to jButtonToEuro. And to finish the design-time property setting, change their text properties to "From Euro" and "To Euro" respectively.

Using IEuro Web Service
Finally, let's write the actionPerformed event handlers for the jButtonFromEuro and jButtonToEuro controls. We need to start with two import statements, for java.net.* and for com.borland.www.* in order to include the classes we need.
In both event handlers, we need to create binding to the IEurobindingStub, which needs a URL pointing to the http://www.eBob42.com/cgi-bin/Euro42.exe/IEuro in order to create the binding.
Next, in the actionPerformed event handler for the jButtonFromEuro, we can use the stub to call the FromEuro method, taking the contents of the jTextFieldEuro control (converted to a Double) as input, and placing the results back in the jTextFieldCurr control.

  void jButtonFromEuro_actionPerformed(ActionEvent e) {
    try {
      IEurobindingStub stub = new IEurobindingStub(
          new URL("http://www.eBob42.com/cgi-bin/Euro42.exe/soap/IEuro"));
      jTextFieldCurr.setText(String.valueOf(
        stub.FromEuro(EuroCurr[jComboBoxCurr.getSelectedIndex()],
          Double.valueOf(jTextFieldEuro.getText()).doubleValue())));
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
The actionPerformed event handler for the jButtonToEuro is very similar. Instead of calling the FromEuro method, we now call the ToEuro method, and we now take the contents of the jTextFieldCurr control as input, and place the result back in the jTextFieldEuro control.
  void jButtonToEuro_actionPerformed(ActionEvent e) {
    try {
      IEurobindingStub stub = new IEurobindingStub(
          new URL("http://www.eBob42.com/cgi-bin/Euro42.exe/soap/IEuro"));
      jTextFieldEuro.setText(String.valueOf(
        stub.ToEuro(EuroCurr[jComboBoxCurr.getSelectedIndex()],
          Double.valueOf(jTextFieldCurr.getText()).doubleValue())));
    }
    catch (Exception ex) {
    ex.printStackTrace();
    }
  }
After these two event handlers we can save the project and hit F9 to run the application, which - if we didn't make any typing mistakes - will result in the Euro Calculator as shown in Figure 6:

Figure 6.
Final Euro Calculator

As you can see, you can enter 42 in the left jTextField, select NLG in the jComboBox and click on the "To Euro" button to produce 19.0587690 in the right jTextField (showing the amount in Euros).

JBuilder 6 Web Services
In this article, I hope to have shown you that it's really easy to consume Web Services in JBuilder 6 Enterprise with the Borland Web Services Kit for Java. If you have any comments or feedback, don't hesitate to let me know by e-mail.


This webpage © 2002-2009 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.