Figure 3   Accessing Remote Objects Using HTTP

Technology
Remote Object
Connection
User Interface
Browser Requirements
Remote Scripting
Script function in an ASP page
An HTTP request issued by a hidden Java language applet
Updated via DHTML scripting
Java and ECMAScript
Remote Data Services
COM component installed on the Web server
A special COM object supporting HTTP
Updated via DHTML scripting
COM support
Data Binding
Connection String
An ActiveX control getting data
Automatically updated via DHTML
Internet Explorer 4.0 and later
XmlHttp
ASP page
A hidden HTTP request issued by the component itself
Updated via XSL or DHTML scripting
Internet Explorer 5.0
Frames
ASP page
An HTTP request issued by the browser
A brand new page
Frame support


Figure 5   RS Remote Call Functions

Function
Description
RSEnableRemoteScripting(url)
Instantiates the Java language applet driving the HTTP conversation with the Web server
RSExecute(asp, method, params, callback, error_callback, context)
Lets you issue both synchronous and asynchronous remote calls through the previously instantiated applet
RSGetASPObject(asp)
Creates and returns a fake object with the same programming interface as the target ASP page


Figure 6   Remote Scripting Demo


 <HTML>
 <LINK rel="stylesheet" href="styles.css">
 <HEAD>
 <TITLE>Employee Info</TITLE>
 </HEAD>
 
 <BODY>
 <script language="JavaScript" src="http://expoware/_ScriptLibrary/rs.htm">
 </script>
 <script language="JavaScript">
 RSEnableRemoteScripting("http://expoware/_ScriptLibrary");
 </script>
 
 <table border=0 cellpadding=0 width=100%>
 <td width=20%><img src="mind.gif"></img></td>
 <td class="heading" width=75%>&nbsp;Employee Info Finder<br>
 <span class=subhead>brought to you by <big>Remote Scripting</big> 
 and <big>DHTML</big></span></td>
 </table>
 
 <br>
 Enter the last name of the employee you want to search:
 <br><input type=text name="txtEmpName" size=40> 
 <input type=button name=btnExecute style="width=150"
        value="Show" 
        onclick="execCall()">
 <hr>
 <br>
 
 <table>
 <tr><td><b>Employee</b></td><td><span id="empName"></td></tr>
 <tr><td><b>Title</b></td><td><span id="empTitle"></td></tr>
 <tr><td><b>City</b></td><td><span id="empCity"></td></tr>
 <tr><td><b>Hired on</b></td><td><span id="empHired"></td></tr>
 </table>
 
 <hr>
 
 <textarea cols=60 row=5 name="errLog">
 </textarea>
 
 <SCRIPT LANGUAGE="javascript">
 
     var serverURL = "http://expoware";
     var pageURL   = "/mind/rs/server/EmpData.asp";
     var aspObject;
 
     function refreshPage(co)
     {
         if (co.status == 0) {
                 a = co.return_value;    
                 empName.innerText = a[1];
                 empTitle.innerText = a[2];
                 empCity.innerText = a[3];
                 empHired.innerText = a[4];
          }
      }
  
     function errHandler(co)
     {
         if (co.status != 0) {
             co.context.value = co.message;
         }
     }
 
     function execCall()
     {
         errLog.value = "";
             var co = RSExecute(serverURL+pageURL, "GetEmployeeInfo", 
                                txtEmpName.value, 
                                refreshPage, errHandler, errLog);
     }
 </SCRIPT>
 
 </BODY></HTML>

Figure 7   RSExecute Arguments

Parameter
Description
AspPage
URL pointing to the ASP page with the JavaScript object to call.
MethodName
Name of the function to be called.
Params
Optional. Zero or more arguments to be passed to the method.
Callback
Optional. Reference to the callback function to be used when calls are asynchronous.
ErrCallback
Optional. Reference to the callback function to be used in case of error during the execution.
Context
Optional. Reference to any external argument you want the callback to receive.


Figure 9   ASP Remote Page


 <%@ LANGUAGE=VBSCRIPT %>
 <% RSDispatch %>
 
 <SCRIPT RUNAT=SERVER Language=javascript>
 <!--#INCLUDE VIRTUAL="/_ScriptLibrary/RS.ASP"-->
 
     function Description()
     { 
         this.GetEmployeeInfo = DoGetEmployeeInfoAsArray;
     }
     public_description = new Description();
 
 
     function DoGetEmployeeInfoAsArray(empName)
     {
          sql = "select * from Employees where [LastName]='" + empName + "'";
          rst = new ActiveXObject("ADODB.Recordset");
          rst.CursorLocation = 3;     // adUseClient
 
          rst.Open(sql, "NW");
          i = 0;
          aRecs = new Array();
          aRecs[i++] = rst.RecordCount + " record(s) found.";
 
          if (rst.RecordCount == 1)  {
             aRecs[i++] = rst.Fields("EmployeeID").Value + " - " +
                          rst.Fields("TitleOfCourtesy").Value + " " + 
                          rst.Fields("FirstName").Value + " " + 
                          rst.Fields("LastName").Value;
             aRecs[i++] = rst.Fields("Title").Value;
             aRecs[i++] = rst.Fields("City").Value + " " + 
                          rst.Fields("Region").Value + ", " +
                          rst.Fields("Country").Value;
             d = new Date(rst.Fields("HireDate").Value);
             aRecs[i++] = (1+d.getMonth()) + "/" + d.getDate() + "/" + d.
                          getYear();
          }
 
          return aRecs;
     }
</SCRIPT>

Figure 13   From Recordset to Array


 <SCRIPT RUNAT=SERVER Language="JavaScript">
 
 // **************************************************************
 // * Rs2Array.js 
 // * Provides a server-side JScript function to convert 
 // * recordsets to arrays
 // **************************************************************
 
 function Recordset2Array(rst) 
 {
     var numOfFields, i, j
     var aRS;
     
     // Creates a new array to hold the RS
     aRS = new Array();
     
     // Adds a first child array with all the field names
     aRS[0] = new Array();        
     numOfFields = rst.Fields.Count;
     
     for (i=0; i<numOfFields; i++)
         aRS[0][i] = rst.Fields.Item(i).Name;
 
     // Adds as many child arrays as needed to hold all the records 
     i = 1;
 
     while (!rst.EOF) {
         aRS[i] = new Array();
         
         for (j=0; j<numOfFields; j++) 
             aRS[i][j] = String(rst.Fields.Item(j).Value);
         
         // Next record and next item in the array
         rst.MoveNext();
         i++;
     }
 
     return aRS;
 }
 
 </SCRIPT>