Logging on to an IBM 3270 host (VBScript)

In this example, which runs only in Internet Explorer, Microsoft's VBScript is used to launch an IBM 3270 session and log on to the host computer. Two <form> fields on the web page are used to get the user's name 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 VBScript WriteLn statements don't replace the contents of the current window and make the rest of the script unavailable. A related example shows a JavaScript version of this script.

To use this example, you must change the hostURL parameter in the <applet> tag that's part of the DoWriteApplet() function below to a host appropriate for your network. In addition, the HTML page must be placed in the session folder rather than in the ReflectionData/deploy folder. If the session were delivered from the deploy folder as a protected session, dynamically generated code that gets added to the applet tag written out by the WriteLn statements would contain nested double quotation marks and would generate a syntax error.

<html>
<head>
<title>Sample Logon Script for IBM 3270 (VBScript)</title>
<script language="VBScript">
<!--
    ' Script-level variables shared by various functions.
    Dim strUserid
    Dim strPword
    Dim intInited
    Dim intCount
    Dim objAppletTarget
    Dim objAPI
    Dim objTargetdoc
    Dim objTargetwin

    ' Copy relevant symbolic constants from
    ' api_ibm3270_constants.js and modify as needed for VBScript.
    Dim ANY_COLUMN
    Dim IBM3270_ENTER_KEY
    Dim IBM3270_TAB_KEY
    ANY_COLUMN = -1
    IBM3270_ENTER_KEY = 35
    IBM3270_TAB_KEY = 52

    '****************************************************
    ' Handle a click on the Logon button. This first
    ' initializes the script-level variables, and then validates
    ' the form inputs. If the form inputs are okay, the
    ' procedure DoWriteApplet is called to write the applet
    ' tag to a new browser window.
    '****************************************************
    Sub Logon_OnClick()
        Call DoInitVariables()
        If blnValidateInputs() = True Then
            Call DoWriteApplet()
        End If
    End Sub
    
    '****************************************************
    ' Initialize the script-level variables.
    '****************************************************
    Sub DoInitVariables()
        strUserid = ""
        strPword = ""
        intInited = 0
        intCount = 0
        objAppletTarget = Null
        objAPI = Null
        objTargetdoc = ""
        objTargetwin = ""
    End Sub
    
    '****************************************************
    ' Check for a username and password, and if either is
    ' not present, display a message box and return False.
    ' If the User ID and Password are filled in, return
    ' True.
    '****************************************************
    Function blnValidateInputs()
        strUserid = Document.LogonForm.Userid.Value
        strPword = Document.LogonForm.Password.Value
        If strUserid = "" Then
            Alert "You must enter a User ID to log on!"
            Document.LogonForm.Username.Focus()
            blnValidateInputs = False
            Exit Function
        End If
        
        If strPword = "" Then
            Alert "You must enter a Password to log on!"
            Document.LogonForm.Password.Focus()
            blnValidateInputs = False
            Exit Function
        End If
        blnValidateInputs = True
    End Function

    '****************************************************
    ' This function writes the applet tag to launch an 
    ' IBM 3270 session in a new browser window called 
    ' "IBM3270Applet." When the new window is done loading
    ' the new window's onLoad event handler is invoked,
    ' which calls the DoLogon procedure in this script.
    '****************************************************
    Sub DoWriteApplet()
        Set objTargetwin = Window.Open( "", "IBM3270Applet", "status=yes, width=650, height=550" )
        Set objTargetdoc = objTargetwin.Document

        objTargetdoc.Open
        objTargetdoc.WriteLn "<html>"
        objTargetdoc.WriteLn "<body language='VBScript' OnLoad='Window.Opener.DoLogon'>"
        objTargetdoc.WriteLn "<h1>Reflection for the Web -- IBM 3270</h1>"
        objTargetdoc.WriteLn "<applet mayscript name='IBM3270'"
        objTargetdoc.WriteLn "        codebase='../ex/'"
        objTargetdoc.WriteLn "        code='com.wrq.rweb.Launcher.class'"
        objTargetdoc.WriteLn "        width='600' height='400'"
        objTargetdoc.WriteLn "        archive='Launcher.jar'>"
        ' Change the hostURL parameter to a host appropriate for your network.
        objTargetdoc.WriteLn "    <param name='hostURL' value='tn3270://payroll'>"
        objTargetdoc.WriteLn "    <param name='autoconnect' value='false'>"
        objTargetdoc.WriteLn "    <param name='frame' value='false'>"
        objTargetdoc.WriteLn "    <param name='preloadJSAPI' value='true'>" 
        objTargetdoc.WriteLn "    <param name='launcher.sessions' value='IBM3270'>"
        objTargetdoc.WriteLn "</applet>"
        objTargetdoc.WriteLn "<form><input type='button' name='Close' value='Close Window' "
        objTargetdoc.WriteLn "OnClick='Window.Close()'></form>"
        objTargetdoc.WriteLn "</body>"
        objTargetdoc.WriteLn "</html>"
        objTargetdoc.Close()
    End Sub

    '****************************************************
    ' This procedure is called by the applet window's onLoad
    ' event handler after the new child window has been loaded.
    ' It gets a reference to the Reflection Launcher applet on the
    ' web page, and if the reference is null, it shuts the 
    ' child window. Otherwise, it calls the DoGetAPI() 
    ' procedure.
    '****************************************************
    Sub DoLogon()
        On Error Resume Next
        Set objAppletTarget = objTargetdoc.IBM3270
        If objAppletTarget = Null Then
            Alert "Could not get applet reference. The script is stopping now."
            objTargetwin.Close()
            Exit Sub
        Else
            DoGetAPI
        End If
    End Sub
       
    '***************************************************
    ' This procedure gets the Reflection JSAPI object for
    ' the terminal session on the web page. If the API
    ' object is null, it rechecks every 2 seconds. When
    ' the API object is available, the procedure then
    ' gets the Reflection session's initialization status,
    ' again pausing 2 seconds between attempts, before calling
    ' DoConnect. For more information about determining 
    ' Reflection's initialization status, see Using an onLoad
    ' Handler to Determine When Reflection Is Initialized.
    '****************************************************
    Sub DoGetAPI()
       Set objAPI = objAppletTarget.getAPI("JSAPI", "IBM3270")
       If objAPI = Null Then
          SetTimeout "DoGetAPI()", 2000
       Else
          On Error Resume Next
          intInited = objAPI.getInitialized()
          If intInited = 0 Then
             If intCount < 10 Then
                intCount = intCount + 1
                SetTimeout "DoGetAPI()", 2000
             Else
                Alert "Unable to initialize Reflection."
             End If
          ElseIf intInited > 0 Then
             DoConnect
          Else
             Alert "Reflection initialization failed."
          End If
       End If
    End Sub
    
    '****************************************************
    ' This procedure starts the actual logon tasks. It 
    ' determines if the session is connected, and if so, 
    ' finds the Userid and Password prompts on the display
    ' and transmits the user's data.
    '****************************************************
    Sub DoConnect()
        ' If there's no connection, try to connect.
        If objAPI.isConnected() = False Then
            objAPI.connect()
        Else
            objTargetwin.Alert "You're already connected!"
            Exit Sub
        End If
        
        ' Use the status line to display informational messages.
        objTargetwin.Status = "Waiting for USERID prompt."

        ' 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.
        If objAPI.waitForDisplayString( "USERID", 19, _
                              ANY_COLUMN, 5000 ) = False Then
            objTargetwin.Alert "Unable to find USERID prompt. The script will stop, " & _
                   "but you may continue manually."
            Exit Sub
        End If
        
        ' Wait 5 seconds for the cursor to appear in row 19, column 16.
        ' This is the location for the user's ID.
        If objAPI.waitForCursorEntered( 19, 16, 5000 ) = False Then
            objTargetwin.Alert "Cursor was not found in the USERID field. The script " & _
                   "will stop, but you may continue manually."
            Exit Sub
        End If
        
        ' 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.
        objAPI.transmitString( strUserid )
        objAPI.transmitTerminalKey( IBM3270_TAB_KEY )
        
        ' Wait for the cursor to enter the Password field.
        objTargetwin.Status = "Waiting for Password prompt."
        If objAPI.waitForCursorEntered( 20, 16, 5000 ) = False Then
            objTargetwin.Alert "Cursor was not found in the Password field. The script will " & _
                     "stop, but you can try completing the logon manually."
            Exit Sub
        End If

        ' Cursor is in Password field, so transmit the user's password, followed
        ' by the Enter key.
        objAPI.transmitString( strPword )
        objAPI.transmitTerminalKey( IBM3270_ENTER_KEY )
        
        
        ' Wait 5 seconds for the "Ready" message. If not found, display
        ' a message box.
        objTargetwin.Status = "Waiting for Ready prompt."
        If objAPI.waitForDisplayString( "Ready;", 5000 ) = False Then
            objTargetwin.Alert "Ready signal not found. The session may not be ready for input."
            Exit Sub
        End If
        
        ' Display a final status message, and set the focus to the
        ' terminal display.
        objTargetwin.Status = "Ready to proceed."
        objAPI.requestDisplayFocus()
        
    End Sub

// -->
</script>
</head>
<body bgcolor="#FFFFFF">
<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"></p>
</form>
</body>
</html>