Logging on to an IBM host

In this example, JavaScript is used to launch an IBM 3270 session and log on to the host computer.

The logon procedure for an AS/400 computer is quite similar to an IBM mainframe, and you could easily modify this script to use an IBM 5250 session.

Two <form> fields on the web page are used to get the user's ID and password before creating the Reflection session applet and performing the logon. The Reflection session is created in a new browser window that has only a status bar. This is done so the JavaScript writeln statements don't replace the contents of the current window and make the rest of the script unavailable. A related example shows a VBScript version of this script for Internet Explorer.

To use this example, you must change the hostURL parameter in the <applet> tag that's part of the writeApplet() function below to a host appropriate for your network.

<html>
<head>
<title>Sample Logon Script for IBM 3270 (JavaScript)</title>
<script language="JavaScript">
<!--
    var userid;
    var pword;
    var targetdoc;
    var targetwin;
    var appletTarget = null;
    var api = null;
    var count = 0;
    var errcount = 0;
    var inited = 0;

    /* 
       Copy relevant symbolic constants from
       api_ibm3270_constants.js and modify as needed for VBScript.
    */
    var ANY_COLUMN = -1;
    var IBM3270_ENTER_KEY = 35;
    var IBM3270_TAB_KEY = 52;
    
    /*
       Set an error handler. The error handler function is at the end of
       the script.
    */
    window.onerror = errHandler;

    /*
       This is the main function that performs the logon tasks.
    */
    function handleLogonButton()
    {
        // Validate the userid and password.
        if ( validateInputs() )
        {
            writeApplet();
        }
    }

    /*
       This function checks for a user ID and password, and if either is
       not present, displays an alert message.
    */
    function validateInputs()
    {
        userid = document.LogonForm.UserID.value;
        pword = document.LogonForm.Password.value;
        if ( userid == "" )
        {
            alert( "You must enter a User ID to log on!" );
            document.LogonForm.UserID.focus();
            return false;
        }
        if ( pword == "" )
        {
            alert( "You must enter a Password to log on!" );
            document.LogonForm.Password.focus();
            return false;
        }
        return true;
    }

    /*
       This function writes the applet tag to launch an IBM 3270 session in
       a new browser window called "IBM3270Applet."
    */
    function writeApplet()
    {
        targetwin = window.open( "", "IBM3270Applet", 
                                 "status=yes, width=650, height=550" );
        targetdoc = targetwin.document;
        
        // Use the "with (object)" statement to simplify the following lines.
        with ( targetdoc ) {
            writeln( '<html>' );
            writeln( '<body onLoad="window.opener.doLogon()">' );
            writeln( '<h1>Reflection for the Web -- IBM 3270</h1>' );
            writeln( '<applet mayscript name="IBM3270"' );
            writeln( '       codebase="./ex/"' );
            writeln( '       code="com.wrq.rweb.Launcher.class"' );
            writeln( '       width="600" height="400"' );
            writeln( '       archive="Launcher.jar">' );
            // Change the hostURL to a value appropriate for your network.
            writeln( '   <param name="hostURL" value="tn3270://payroll">' );
            writeln( '   <param name="autoconnect" value="false">' );
            writeln( '   <param name="frame" value="false">' );
            writeln( '   <param name="launcher.sessions" value="IBM3270">' );
            writeln( '   <param name="preloadJSAPI" value="true">' );
            writeln( '</applet>' );
            writeln( '<form>' );
            writeln( '<input type="button" ' );
            writeln( '       name="Close" value="Close Window" ' );
            writeln( '       onClick="window.close()"></form>' );
            writeln( '</body>' );
            writeln( '</html>' );
            close();
        }
    }

    /*
       This function is called by the applet window's onLoad event 
       handler after the new child window has been loaded. This function
       is the main entry point for the rest of the logon processing. It 
       works similar to the tryInit() function in the example
       Using an onLoad Handler to Determine When Reflection Is Initialized.
       Once Reflection is initialized, the logon procedure continues.
    */
    function doLogon()
    {
        appletTarget = targetdoc.IBM3270;
        api = appletTarget.getAPI("JSAPI","IBM3270");
        if ( api != null )
        {
            inited = api.getInitialized();
            if ( inited > 0 )
            {
                doConnect();
            }
            else if ( inited < 0 )
            {
                alert( "Reflection initialization failed." );
            }
        }
        else
        {
            if ( count < 10 )
            {
                count++;
                setTimeout( "doLogon()", 1000 );
            }
            else
            {
                alert( "Unable to initialize Reflection." );
            }
        }
    }
    
    /*
       This function starts the actual logon tasks. It determines if the session
       is connected, and if so, calls the function that finds the userid and 
       password prompts on the display and transmits the user's data.
    */
    function doConnect()
    {
        // If there's no connection, try to connect.
        if ( !api.isConnected() )
        {
            targetwin.status = "Connecting...";
            api.connect();
        }
        else
        {
            alert( "You're already connected!" );
            return;
        }

        // Use the status line to display an informational message, and then
        // wait 5 seconds for the USERID prompt to appear on the display.
        // The coordinates in which to start looking are any column in 
        // row 19. The API uses 0-based coordinates, in contrast to
        // the 1-based coordinates that are used for the status line.
        targetwin.status = "Finding USERID prompt.";
        if ( !api.waitForDisplayString( "USERID", 19,
                               ANY_COLUMN, 5000 ) )
        {
            targetwin.alert( "Unable to find USERID prompt. The " +
                   "script will stop, but you may continue manually." );
            return;
        }
        
        // Wait 5 seconds for the cursor to appear in row 19, column 16.
        // This is the location for the user's ID.
        targetwin.status = "Waiting for cursor to enter USERID field.";
        if ( !api.waitForCursorEntered( 19, 16, 5000 ) )
        {
            targetwin.alert( "Cursor was not found in the USERID field. " +
                   "The script will stop, but you can try completing the " +
                   "logon manually." );
            return;
        }
        
        // userid field was found, and cursor is in place, so transmit the 
        // user's ID, followed by the Tab key to move to the next field.
        api.transmitString( userid );
        api.transmitTerminalKey( IBM3270_TAB_KEY );
        
        // Wait for the cursor to enter the password field.
        targetwin.status = "Waiting for the cursor to enter the Password field.";
        if ( !api.waitForCursorEntered( 20, 16, 5000 ) )
        {
            targetwin.alert( "Cursor was not found in the Password field. " +
                     "The script will stop, but you can try completing " + 
                     "the logon manually." );
            return;
        }

        // Cursor is in Password field, so transmit the user's password, 
        // followed by the Enter key.
        api.transmitString( pword );
        api.transmitTerminalKey( IBM3270_ENTER_KEY );
        
        // Wait 5 seconds for the "Ready" message to appear on the display.
        // If the "Ready" message is not found, display an alert message.
        targetwin.status = "Waiting for Ready prompt.";
        if ( !api.waitForDisplayString( "Ready;", 5000 ) )
        {
            targetwin.alert( "Ready signal not found. The session may not " +
                             "be ready for input." );
            targetwin.status = "";
            return;
        }

        // Done with logon, so display final status message and set
        // focus to the terminal display.
        targetwin.status = "Ready to proceed.";
        api.requestDisplayFocus();
    }
    
    /*
       This is the error handler function that will catch script errors.
       The error we're most interested in may occur when trying to
       get Reflection's initialized state in the doLogon() function.
    */
    function errHandler( msg, url, line )
    {
        if ( inited <= 0 ) {
            if ( errcount < 10 ) {
                errcount++;
                setTimeout( "doLogon()", 2000 );
            }
            else {
                alert( "Unable to initialize Reflection." );
            }
        }
        return true;
    }
// -->
</script>
</head>
<body>
<h1>Reflection for the Web -- IBM 3270 Logon</h1>
<!--
   Use <form> items to accept the User ID and Password, and to create
   a Logon button.
-->
<form name="LogonForm">
<p>User ID: <input type="text" name="UserID" default size="25"></p>
<p>Password: <input type="password" name="Password" size="25"></p>
<p><input type="button" Name="Logon" value="Logon" onClick="handleLogonButton()"></p>
</form>
</body>
</html>