Connecting a printer session

In this example, an IBM 3270 Printer session is embedded in the browser window, and JavaScript is used to connect to the host computer. An HTML form list lets the user select the name of the printer device to which to connect, and two buttons let the user connect and disconnect the session.

This example can easily be modified to connect to an AS/400 Printer session. One difference is that it is not necessary to specify a device name when connecting to an AS/400 Printer session (although the AS/400 Printer applet can be configured to require a device name from the user by setting the promptForDeviceName parameter to True).

To use this example, you must change the hostURL parameter (including the port number) in the <applet> tag below to a host appropriate for your network. You must also change the device names in the <form> tag to appropriate device names.

<html>
<head>
<title>Sample Script for IBM 3270 Printer Emulation (JavaScript)</title>
<script language="JavaScript">
<!--
    var api = null;
    var deviceName = "";

   /*
       The jsapiInitialized function is called by Reflection
       when it has finished its initialization. Reflection
       passes the function a reference to the JavaScript API.
    */
    function jsapiInitialized( jsapi )
    {
       api = jsapi;
    }

    /*
       This function handles a click in the Connect button.  If there's no
       connection already established, it calls the doConnect() function to
       make a connection attempt. If there is a connection already, an alert
       message is displayed.
    */
    function handleConnectButton()
    {
        if ( api.isConnected() )
        {
            alert( "You already have a connection to '" +
                   api.getString( "deviceName" ) + "'. You must first\n" +
                   "disconnect the current connection before " +
                   "establishing a new\nconnection.");
            return; 
        }
        doConnect();
    }

    /*
       This function handles a click in the Disconnect button. If there is
       no current connection, it does nothing. Otherwise, it disconnects
       the current session and displays a message in the form field.
    */
    function handleDisconnectButton()
    {
        if ( api.isConnected() )
        {
            api.disconnect();
            document.DevicePicker.ConnectedDevice.value = "<disconnected>";
        }
    }

    /*
       This function establishes the connection. It gets the name of the
       item selected in the form list, sets the Reflection session's device
       name to the same name, and then opens the connection. After the connection
       is opened, a timer is set to check the connection status after 2
       seconds (2000 milliseconds), using the setTimeout() method.
    */
    function doConnect()
    {
        var deviceList = document.DevicePicker.DeviceName;
        deviceName = deviceList.options[deviceList.selectedIndex].text;
        api.setString( "deviceName", deviceName );
        api.connect();
        setTimeout( "checkConnected()", 2000 );
    }

    /*
       This function checks whether the connection to the host using the
       selected device name was successful or not. If the connection failed,
       Reflection will display an alert message. This function will update
       the document's text field with either a success or failure message.
    */
    function checkConnected()
    {
        var isConnected = api.isConnected();
        var theDevice = document.DevicePicker.ConnectedDevice;
        if ( !isConnected )
        {
            theDevice.value = "<failed>";
        }
        else if ( isConnected )
        {
            theDevice.value = api.getString( "deviceName" );
        }
    }
// -->
</script>
</head>
<body>
<h1>Reflection for the Web -- IBM 3270 Printer: Session Picker</h1>
<p>
<!--
   Create a basic IBM 3270 Printer applet on the page, but don't
   connect until the user selects a device name and clicks the
   Connect button. 
   
   To use this example, change the hostURL parameter to one 
   appropriate for your network (including the port number), and
   change the names of the devices to devices for your host.
-->
<applet mayscript name="IBM3287"
        codebase="./ex/"
        code="com.wrq.rweb.Launcher.class"
        width="450" height="160"
        archive="Launcher.jar">
    <param name="hostURL" value="tn3270e://ibmprinter:5002">
    <param name="autoconnect" value="false">
    <param name="frame" value="false">
    <param name="launcher.sessions" value="IBM3287">
    <param name="preloadJSAPI" value="true">
</applet>
</p>
<!--
   Use <form> items to display a list from which the user
   can select a device to connect to and buttons to connect and
   disconnect the session.
-->
<form name="DevicePicker">
<p>
Device name: <select name="DeviceName">
<option selected>PRINTDEV1</option>
<option>PRINTDEV2</option>
<option>PRINTDEV3</option>
<option>PRINTDEV4</option>
</select>
<input type="button" name="Connect" value="Connect" onClick="handleConnectButton()">
<input type="button" name="Disconnect" value="Disconnect"
                     onClick="handleDisconnectButton()">
</p>
<p>
Connection established to device: <input type="text" 
            name="ConnectedDevice" value="<disconnected>" disabled>
</p>
</form>
</body>
</html>