wayne plourde

software architect mcad mcsd scjp

home | articles | book | resume | projects | contact

12 - Remote Scripting (Outline and Notes)

ASP 3.0 The Complete Reference

Chapter 12

Remote Scripting (Outline and Notes)

Why Remote Scripting?

Interactive Communication with Server

XML HTTPREQUEST object is more robust

Works in Netscape!!!!!!!!

Remote scripting allows you to work in client script, but call methods (function or subroutines) in an ASP page. In effect, you can call server scripts as if they were local routines, but they run on the server, with full access to the capabilities of the server. Because you never leave the current page to call the server script, the state of the page is maintained.

Other methods for Remote Scripting

RDS

XML

Data Binding

Limitation Client to Server uses HTTP GET which is limited to 2K

What is Remote Scripting

Remote scripting is implemented as a library of functions that you call from client script when you want to run a server method. When you call a server method, the request is routed to a proxy process that runs asynchronously in the browser (in the current implementation, the proxy is implemented as a Java applet.) The proxy process sends a request to the server for the ASP page containing the method you have called.

The server loads the ASP page, and a special routine on the ASP page dispatches your requests to the correct function. If the method returns a value, it is sent back to the proxy process, which packages it as an object - a call object - containing properties for the return value and other useful information.

When you make a call in client script to a server method, you can choose to do so in two ways:

Synchronously   Your script calls the remote procedure and waits for it to return. This is useful if you need the results of the remote procedure before you proceed.

Asynchronously   Your script makes the call to a remote script, and then continues processing. The page remains available for users to work with. Asynchronous calls are useful when a call might take a long time.

RPC Concepts

What you need

Download

msdn.microsoft.com/scripting

1.0b

Text Box: Diagram showing relation of componentsComponents for Remote Scripting

Figure 1: Diagram of Remote Scripting

To implement remote scripting, you need the following files in addition to your own client (.htm) file and server (.asp) files:

  • Rs.htm Contains the methods you use in your .htm file to initialize remote scripting, execute remote procedures, check the status of remote calls, and get method results.
  • Rs.asp Contains methods called from your .asp file to initialize remote scripting on the server side and to dispatch to the appropriate function in your page.
  • Rsproxy.class Contains the Java class files (object code) for the applet that communicates between client and server pages.

These files act as libraries that you can use from within your own files. In general, you simply include the relevant files (Rs.htm or Rs.asp) in your client or server pages, and then call methods as needed. For details about how to do this, see Enabling Remote Scripting in Client Pages and Enabling Remote Scripting in Server Pages.

In your client page, you reference Rs.htm, which makes the methods in that file available to your server script. Among those methods are calls to the Rsproxy applet. When you create the server page, you include a server-side INCLUDE statement that references the Rs.asp file. As with the Rs.htm in the client file, this makes the required methods available on the server page.

All of the files must be available on the server. You can place them anywhere that is convenient. However, the path must be available to your client and server files when they are requested from the server. By default, the remote scripting procedures assume that these files are available in a folder called _ScriptLibrary off the virtual root of your server or project. If you do not place them there, note their path carefully, because you will need to specify it when creating the client and server pages.

The Example - Web Based Stock Ticker

Figure 2:Screen Shot of Stock Ticker

Client

1

2

3

4

5

Server

1

2

3

4

5

Enabling Remote Scripting in Client Pages

Before you can use remote scripting to call server scripts, you must add remote scripting capability to your client page. The routines required to use remote scripting from client script are contained in the file Rs.htm. You must include this file in your client page. In addition, you must call a method to start the remote scripting applet.

To enable remote scripting

Create an empty JavaScript script block that references the Rs.htm file, as in the following example:

<SCRIPT LANGUAGE="JavaScript" src="../_ScriptLibrary/RS.HTM">

Be sure to specify the correct path for the Rs.htm file. This script block can appear anywhere in your client page, but must be in a block that is executed before you call a remote procedure.

Create another JavaScript script block, and in that block call the method RSEnableRemoteScripting. By default, this method assumes that the Rsproxy.class applet is in a folder called _ScriptLibrary immediately below the virtual root of your server or project. If it is not, you must pass the path to the Rsproxy.class file in the method call.

This script block containing RSEnableRemoteScripting must appear in the body portion of the document, because it creates the <APPLET> tag containing the reference to Rsproxy.class. It must follow the script block that includes Rs.htm. You might place it immediately following the <BODY> tag, as in the following example:

HTML

1

2

3

4

5

<BODY>

<SCRIPT LANGUAGE="JavaScript">

RSEnableRemoteScripting("../_ScriptLibrary")

</script>

<!-- remainder of .htm file here -->

Results:

Note The <APPLET> tag created by RSEnableRemoteScripting does not appear in your page, even if you view the page's source in your browser.

A skeleton client page with remote scripting enabled looks like this:

HTML

1

2

3

4

5

6

7

8

9

10

<HTML>

<HEAD>

<TITLE>Remote Scripting Test</TITLE>

</HEAD>

<BODY>

<SCRIPT LANGUAGE="JavaScript" src="../_ScriptLibrary/RS.HTM"></script>

<SCRIPT LANGUAGE="JavaScript">

RSEnableRemoteScripting("../_ScriptLibrary")

</SCRIPT>

<H1>Remote Scripting Client Page</H1>

This is text in the remote scripting sample client page.

</BODY>

</HTML>

Results:

After setting up the client page in this way, you can add scripts that call remote procedures on server pages. For details about doing so, see Calling Remote Scripting Methods Synchronously and Calling Remote Scripting Methods Asynchronously. For details about setting up a server page to receive remote scripting calls, see Enabling Remote Scripting in Server Pages.

Enabling Remote Scripting in Server Pages

In addition to configuring client pages to call remote scripts, you must configure your server page to receive them. Doing so involves these steps:

Including the remote scripting server library and initializing it.

Writing functions or subroutines that the client page can call.

Exposing your functions and subroutines as methods.

By default, an ASP page called by client script is not displayed in the browser - instead, it is simply executed on the server and the results sent to the client. Therefore, as a rule you do not need to include any HTML text in the ASP page, only script. However, it is possible to call methods on an ASP page that includes HTML text and is displayed normally, as long as you initialize the remote scripting server library and expose the page's methods using the techniques described below.

To include and initialize the remote scripting server library

Create a server-side INCLUDE statement that references the Rs.asp file, as in the following example:

<!--#INCLUDE FILE="../_ScriptLibrary/RS.ASP"-->