Creating the application  Chapter 3: Creating C++ Components and Clients

Chapter 2: Creating CORBA Java Components and Clients

Creating an applet client

These optional steps describe how to create an applet client that runs in a Web browser. Complete all steps under “Creating the application” before running the applet:

  1. Verify that your browser can run JDK 1.2 applets.

  2. Prepare your EAServer installation to run applets.

  3. Write the applet code.

  4. Create an HTML page to run the applet.

  5. Run the applet.


Verify that your browser can run JDK 1.2 applets

The EAServer client runtime requires JDK 1.2 or later.

To run the tutorial applet, you must have Web browser that is compatible with JDK 1.2 or a later JDK version. Most current browsers include a JDK 1.1 virtual machine. To run JDK 1.2 applets in these browsers, you must use the Java Plug-in available from Sun Microsystems at the Java Plug-in Web site.

The tutorial was tested with Netscape Navigator 4.6.x, 4.7.x, and Internet Explorer 5.0, all using Sun’s Java Plug-in, version 1.3.

NoteThe Java Plug-in is required To run this tutorial without the Java Plug-in, you must edit the HTML code that loads the applet. The tutorial HTML code will not run the applet in browsers where the plug-in is not installed. The Java Plug-in may not be available on your UNIX platform; in that case use a browser running on a supported platform such as Windows NT, Windows 2000, or Sun Solaris.

If you are running the sample applet on a machine where EAServer has been installed, you may need to configure your browser to eliminate conflicts between EAServer classes that are downloaded with the applet and classes that are loaded from the local CLASSPATH setting. The simplest way to do this is to run the browser from a batch file or UNIX script as described here.

StepsConfiguring your browser on Windows

This procedure is necessary only if you have EAServer classes in the system or user CLASSPATH environment variable, or you start the browser from a command line where CLASSPATH has been set to include EAServer classes.

  1. Locate the browser executable, and create a batch file in the same directory with the same base file name, for example netscape.bat or iexplore.bat.

  2. Add these commands to the batch file, where browser.exe is the name of the browser executable:

    @ECHO OFF
    SETLOCAL
    SET CLASSPATH=""
    START browser.exe %*
    ENDLOCAL
    
  3. Change shortcuts that run the browser so that they launch the batch file instead of the browser executable.

StepsConfiguring your browser on UNIX

This procedure is necessary only if you have EAServer classes in the CLASSPATH when you start the browser process. For example, you may have modified the CLASSPATH setting in your .login or .cshrc file.

  1. Create a script to run the browser, for example, ~/bin/netscape.

  2. Edit the script to set the CLASSPATH environment variable to an empty string before running the browser. For example (this script also sets the NPX_PLUGIN_PATH variable to configure the Sun Java Plug-in. See the Java Plug-in documentation for more information):

    #!/bin/sh
    set CLASSPATH="" export CLASSPATH
    set NPX_PLUGIN_PATH=/opt/jre/1.3/plugin/sparc export NPX_PLUGIN_PATH
    /opt/netscape/bin/netscape $*
    
  3. Run this script to start your browser.


Prepare your EAServer installation to run applets

As installed, EAServer does not provide the classes required to run applets under the html or html/classes directories. To run the applet, you must copy or link the required runtime JAR files to the EAServer html/classes directory.

NoteDo not expand the JAR files The applets created in this tutorial do not require expansion of the easclient.jar and easj2ee.jar files.

StepsOn Windows, copy the JAR files

  1. Copy easclient.jar and easj2ee.jar from the EAServer java/lib directory to the html/classes directory.

StepsOn UNIX platforms, link the JAR files

  1. Create symbolic links from the easclient.jar and easj2ee.jar in the EAServer java/lib directory to the html/classes directory. For example, run these commands:

    cd $JAGUAR/html/classes
    ln -s $JAGUAR/java/lib/easclient.jar
    ln -s $JAGUAR/java/lib/easj2ee.jar
    

Write the applet code

In the html\classes subdirectory of your EAServer installation, create a new directory called TutorialApps if it does not exist. In this directory, create the Java file below as JAClient.java.

You can find a copy of JAClient.java in the html/docs/tutorial/java-corba subdirectory of your EAServer installation. Here is the source for JAClient.java:

//
// This is a sample client applet that invokes the 
// JavaArithmetic component created in the EAServer
// Java component tutorial.
//
package TutorialApps;

import org.omg.CORBA.*;
import SessionManager.*;
import java.awt.*;

import Tutorial.*;  // Package for EAServer stub classes

public class JAClient extends java.applet.Applet {

    // User interface controls
    Button mult_button;
    TextField m1_text;
    TextField m2_text;
    TextField  result_text; 

    // Component stub instance
    Tutorial.JavaArithmetic _comp = null;


    public void init() 
    {

        // Draw GUI controls
        m1_text = new TextField("      ");
        m1_text.setText("2.5");
        this.add(m1_text);
        Label l1 = new Label("*");
        this.add(l1);
        m2_text = new TextField("      ");
        m2_text.setText("3.1");
        this.add(m2_text);
        Label l2 = new Label("=");
        this.add(l2);
        result_text = new TextField("      ");
        result_text.setEditable(false);
        this.add(result_text);
        mult_button = new Button("Multiply");
        this.add(mult_button);
        
        //
        // Initialize the CORBA client-side ORB and 
        // obtain a stub for the EAServer component instance.
        //
        System.out.println("... Creating session.");

        //
        // Initialize the ORB.
        //
        java.util.Properties props = new java.util.Properties();
        props.put("org.omg.CORBA.ORBClass", "com.sybase.CORBA.ORB");
        ORB orb = ORB.init(this, props);

        //
        // Create an instance of the EAServer SessionManager::Manager 
        // CORBA IDL object.
        //
        String ior = this.getParameter("ior");
        Manager manager = ManagerHelper.narrow(
                          orb.string_to_object(ior));

        //
        // Create an authenticated session with user "Guest" and password
        // "GuestPassword".
        // 
        Session session = manager.createSession("Guest", "GuestPassword");

        System.out.println("... Creating component instance.");

        //
        // Create a stub object instance for the 
        // Tutorial/JavaArithmetic EAServer component.
        //
        _comp = Tutorial.JavaArithmeticHelper.narrow(
                    session.create("Tutorial/JavaArithmetic"));

        if (_comp == null)
        {
          System.out.print("ERROR: Null component instance. ");
          System.out.print(
    "Check Jaguar Manager and verify that the component ");
          System.out.print(
    "Tutorial/JavaArithmetic exists and that it implements the ");
          System.out.println(
    "Tutorial::JavaArithmetic IDL interface.");
          return;
        }

        System.out.println("... Created component instance.");

    } // init()

    // Handle button clicks
    public boolean action(Event e, java.lang.Object arg) {
        if (e.target == mult_button) {
            doMultiply();
            return true;
        }
        else return false;
    }

    // Call the multiply method and update the displayed result
    private void doMultiply() {

        // Harvest user input
        Double d1 = null;
        Double d2 = null;
        try {
            d1 = new Double(m1_text.getText());
            d2 = new Double(m2_text.getText());
        } catch (NumberFormatException nfe) 
        {
            this.showStatus("ERROR: Bad number format.");
            result_text.setText("");
            return;
        }
        
        // Call the server component method        
        double m1 = d1.doubleValue();
        double m2 = d2.doubleValue();
        try {
            double result = _comp.multiply(m1, m2);
            result_text.setText((new Double(result)).toString());
        }
        catch (org.omg.CORBA.SystemException se)
        {
            System.out.println(
               "Exception executing multiply method: " 
               + se.toString() );
            se.printStackTrace();
        }
         
    } // doMultiply

}

Compile the applet source using a JDK 1.2 or later compiler, for example:

%JAGUAR%\bin\jc JAClient.java

Create an HTML page to run the applet

Copy the file html/docs/tutorial/java-corba/runjavatut.html to the html/classes/TutorialApps subdirectory in your EAServer installation. This file has the OBJECT and EMBED tags required to launch the applet in the Sun Java Plug-in:

<html><body bgcolor="#FFFFFF">
<head><title>This Applet runs the  EAServer tutorial Java component.</head></title>
<hr>
<center>

<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
WIDTH = 600 HEIGHT = 400  codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">
<PARAM NAME = CODE VALUE = "TutorialApps/JAClient.class" >
<PARAM NAME = CODEBASE VALUE = "/classes" >
<PARAM NAME = ARCHIVE VALUE = "easj2ee.jar,easclient.jar" >
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">
<PARAM NAME="scriptable" VALUE="false">
<PARAM NAME="ior" VALUE ="iiop://:9000">
<COMMENT>
<EMBED type="application/x-java-applet;version=1.3"  
   CODE = "TutorialApps/JAClient.class" 
   CODEBASE = "/classes" 
   ARCHIVE = "easj2ee.jar,easclient.jar" 
   WIDTH = 600 HEIGHT = 400 
   ior = "iiop://:9000"
   scriptable=false 
   pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html">
<NOEMBED>
</COMMENT>
<h2>This would be a Cool Applet, but you are not running a Java enabled browser...</h2>
</NOEMBED>
</EMBED>
</OBJECT>

</center>
<hr>
</body></html>

No changes are required to this HTML file if your server is configured to use the default IIOP port number, 9000. If you (or your administrator) changed the IIOP port number after installing EAServer, edit the port number in the HTML file to match. You must edit the port number in both the OBJECT and the EMBED tag. In the OBJECT tag, the port number appears in this line:

<PARAM NAME = "ior" VALUE ="iiop://:9000">

In the EMBED tag, the port number appears in this line:

   ior = "iiop://:9000"

About the OBJECT and EMBED tags

You can skip this section if your only goal is to run the tutorial applet. If you are modifying the tutorial code to run your own applets, you need to understand these tags.

The OBJECT tag is required to load the applet in Internet Explorer. The EMBED tag is required to load the applet in Netscape. You can run Sun’s HtmlConverter tool to convert standard APPLET tags to equivalent OBJECT and EMBED tags. HtmlConverter is available at the Java Plug-in Web site.

The OBJECT and EMBED tags contain similar parameters. See the Java Plug-in Web site for full documentation of the tag formats. The parameters of interest for this tutorial are:


Run the applet

If you have not restarted or refreshed the server since creating the JavaArithmetic component, refresh the server before running the applet.

StepsRunning the applet

  1. Start your Web browser. If necessary, you can run the applet on a different machine than the server host, as long as your server uses a real host address and not localhost or 127.0.0.1.

  2. Connect to the following URL, substituting your server’s host name for host:

    http://host:8080/classes/TutorialApps/runjavatut.html
    

    As installed, EAServer uses 8080 as the HTTP port number. If your server uses a different HTTP port number, change the port number in the URL to match.

  3. Enter numeric values to be multiplied, then click the Multiply button to invoke the multiply component method. The return value is displayed.


Debugging

If everything is working, you should be able to download the applet, watch as it is drawn, then invoke the multiply method by clicking the applet’s Multiply button. If not, check the browser’s Java console for error messages. Also check the server log file (Jaguar.log in the EAServer bin subdirectory).

If the applet connects to the server and instantiates the component successfully, you see ... created session and ... created component instance in the browser’s Java console.





Copyright © 2005. Sybase Inc. All rights reserved. Chapter 3: Creating C++ Components and Clients