WebDocs Home | Show Navigation Hide Navigation

Appendix E: xfNetLink Java Sample Code

The code samples in this appendix illustrate how to create a program using either JavaServer Pages or Java. Both programs call a subroutine named HELLO on the server.

Client application (hello.jsp and hello.java)

The hello.jsp program uses the jsp:useBean action to load a JavaBean created using the xfNetLink Java component generation tools. It sets the host and port, calls the connect() method to make a connection to xfServerPlus, and then calls the hello_routine() method, passing two parameters. The program returns the results and then calls the disconnect() method to close the connection. The hello.jsp page is called from the hello.html page, which includes a table that displays the results to the screen. The code for errorpage.jsp is also included below: hello.jsp sets errorpage.jsp as the page to go to if it encounters a Java exception.

The hello.java program creates a new JavaTest object, sets the host and port, and calls the connect() method to make the connection to xfServerPlus. It then calls the hello_routine() method, passing two parameters, and displays the returned results to the screen. The connection is closed by the disconnect() method.

If you want to try running these programs, see the instructions following the code sample. These sample files are included in the Examples directory in your xfNetLink Java distribution.


<%@ page errorPage="errorpage.jsp" %>
<jsp:useBean id="helloexm" scope="session" class="JavaTest.JavaTest" /> 
<title>Hello Example </title>
  String name = new String("");
  StringBuffer message = new StringBuffer("");
  int rtnval = 0;
  String errmsg = "";
  name = request.getParameter("UserName"); 

  // Enter xfpl name or IP address and port number

  // Connect to xfServerPlus and call hello method
  helloexm.hello_routine(name, message);

  out.println("The hello example returned message = ");

  // shut down xfpl connection


<head> </head>
<form action=hello.jsp method=post>
<table align=center bgcolor=#cccccc border=0    cellPadding=1
cellspacing=0 width=75%>
        <td colspan=2>
            <div align=center><strong><em> <font color=#0000ff face=""
             size=5>Hello Example!</font></em></strong></div>
        <td align=right>name:</td>
        <td><INPUT id=text1 name=UserName MAXLENGTH=10 size=10></td>
        <td align=left> &nbsp; </td>
        <td align=left> &nbsp; </td>
        <td colspan=2><br><hr width=95%></td>
        <td align=left> &nbsp; </td>
        <td align=left><br>
        <input type="submit" value="  Login  " id=submit1 name=runLogin 
         align=center style="HEIGHT: 67px; TOP: 55px; WIDTH: 73px"> 


<body text="red">
<%@ page isErrorPage="true" %>
<! Use the implicit exception object, which holds a >
<! reference to the thrown exception. >
The errorpage  Error: <%= exception.getMessage() %> has been reported. 


import java.io.*;
import JavaTest.*;
import org.omg.CORBA.*;
import Synergex.util.*;

public class hello
    static JavaTest tst = new JavaTest();
    public static void main(String argv[]) 
        hello test = new hello();

        // Open the xfpl connection 
        String name = new String("World");
        StringBuffer message = new StringBuffer("");

        // Make the call
        tst.hello_routine(name, message);
        System.out.println("message = " + message);
       // Close down the xfpl connection 
      } catch (xfJCWException e) {

Serverside code (HELLO subroutine)

The HELLO subroutine is the routine on the server that is called remotely by hello.jsp and hello.java. The hello.dbl file is included in the dbl\examples directory in your Synergy/DE distribution.

.subroutine hello
     a_name       ,a
     a_message    ,a
.define     HELLO       ,"Hello "
.define     NONAME      ,"No name passed "
     if (^passed(a_name)) then
          a_message = %atrim(HELLO + a_name)
          a_message = NONAME

Running the hello program

1.Create an ELB or shared image named hello.elb containing the HELLO subroutine. The HELLO subroutine is located in the dbl\examples directory. Put the ELB on your xfServerPlus machine.

2.Use the Method Definition Utility to add the HELLO subroutine to the SMC. (See “Using the MDU to define Synergy methods” for instructions.) Include the following information in your MDU entry:

Method name = hello_routine

Interface name = JavaTest

Method ID = hello_routine (this is copied from the method name)

Routine name = hello

ELB/shared image name = DBLDIR:hello (change the logical if necessary)

Return type = No return value

The subroutine has two parameters, name and message. Set them up as follows:

Parameter name = name

Data type = Alpha

Length = 20

Data passed = In

Pass by = Descriptor


Parameter name = message

Data type = Alpha

Length = 30

Data passed = In/Out

Pass by = Descriptor


3.Using Workbench or the command line utilities, create a Java JAR file named JavaTest that includes the JavaTest interface. See “Creating a Java JAR file in Workbench” or “Creating a Java JAR file from the command line” for instructions.

4.If xfServerPlus is not already running, start it on the server machine. See one of the following for details:

“Running xfServerPlus on Windows”

“Running xfServerPlus on UNIX”

“Running xfServerPlus on OpenVMS”

5.Deploy and run the client program.

If you’re running the JSP application, do the following:

Put the JAR file, JSP files, and HTML file in the location required by your servlet container.

Verify that your web server and servlet container are running.

Start the web browser on the client machine and load the hello.html page.

If you’re running the Java application, do the following:

Compile the hello.java file, and then put it and the JAR file in a location where Java can find them.

Set your classpath.

Run the program.

See “Setting up your environment for development” if you need help with deployment.