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.

hello.jsp

<%@ page errorPage="errorpage.jsp" %>
<jsp:useBean id="helloexm" scope="session" class="JavaTest.JavaTest" /> 
<htm>
<head>
<title>Hello Example </title>
</head>
<body>
<center>
<%
  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
  helloexm.setxfHost("hostIP"); 
  helloexm.setxfPort(2356); 
  // Connect to xfServerPlus and call hello method
  helloexm.connect();
  helloexm.hello_routine(name, message);
  out.println("<br>");
  out.println("The hello example returned message = ");
  out.println(message);
  out.println("<br>");
  // shut down xfpl connection
  helloexm.disconnect();
%> 
</body>
</htm>

hello.html

<htm>
<head> </head>
<body>
<form action=hello.jsp method=post>
<p>
<table align=center bgcolor=#cccccc border=0    cellPadding=1
cellspacing=0 width=75%>
    <tr>
        <td colspan=2>
            <div align=center><strong><em> <font color=#0000ff face=""
             size=5>Hello Example!</font></em></strong></div>
        <br>
        </td>
    </tr>
    <tr>
        <td align=right>name:</td>
        <td><INPUT id=text1 name=UserName MAXLENGTH=10 size=10></td>
        <br>
    </tr>
    <tr>
        <td align=left> &nbsp; </td>
        <td align=left> &nbsp; </td>
        <br>
    </tr>
    <tr>
        <td colspan=2><br><hr width=95%></td>
    </tr>
    <tr>
        <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"> 
        <br>
        </td>
    </tr>
</table></p>
</form>
</body>
</htm>

errorpage.jsp

<htm>
<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. 
</body>
</htm>

hello.java

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[]) 
    {
      try 
      {
        hello test = new hello();
        // Open the xfpl connection 
        tst.setxfHost("hostIP");
        tst.setxfPort(2356);
        tst.connect();
        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 
       tst.disconnect();
      } catch (xfJCWException e) {
            e.printStackTrace(System.err);
      }
     }
}

Server‑side 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
;Arguments
     a_name       ,a
     a_message    ,a
.define     HELLO       ,"Hello "
.define     NONAME      ,"No name passed "
.proc
     if (^passed(a_name)) then
          a_message = %atrim(HELLO + a_name)
     else
          a_message = NONAME
     xreturn
.end

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

Required

Parameter name = message

Data type = Alpha

Length = 30

Data passed = In/Out

Pass by = Descriptor

Required

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:
5. Deploy and run the client program.

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