When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Friday, April 26, 2002

Simulate Server.Execute and Server.Transfer in ASP 2.0

By Francis Chau

ASP 3.0 provides two useful functions not available on ASP 2.0: Server.Execute and Server.Transfer. In this article, I'll demonstrate a simple technique to simulate these functions in ASP 2.0 and give a few pros and cons on its usage in general. If you are not familiar with Server.Execute and Server.Transfer, read A Look at ASP 3.0 For More Information for a good intro. Also, it would be to your benefit to read the article, Understanding VBScript: Runtime Code Evaluation by Dino Esposito at Windows Scripting Solutions. Dino Esposito's article provides a lot of detail about VBScript's Execute, ExcuteGlobal, and Eval statements. Finally, if you are curious as to what version of ASP you are using, be sure to check out this FAQ. Let's get started!

- continued -

Simulating Server.Execute and Server.Transfer
The Server.Execute function accepts a URL parameter, stops execution of the current page (where the call to the function is made), and transfers the current environment to the new page. When that new page finishes execution, then control returns to the calling page just after where the Server.Execute was called. (Server.Transfer works the same, however, upon completion of the execution of the new page, processing ends.) To simulate this functionality in ASP 2.0, we need to do the following:

  1. Obtain the file contents of target page to execute
  2. Use VBScript's Execute statement (or ExecuteGlobal statement) to process the file contents of our target page. (For more information on Execute be sure to read: Using the Eval and Execute Functions.)
First, let's write a function to obtain the file contents of the page and prepare it to be processed by the Execute statement:

Listing 1 (page1.asp)
Function GetFileContentsForExecution(sTargetFile)
  Dim oFSO, sContents
  'Obtain a reference to the FileSystemObject
  Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
  'Obtain the file contents
  sContents = oFSO.OpenTextFile(Server.MapPath(".") & _
                                 "\" & sTargetFile).ReadAll
  Set oFSO = Nothing 'reference to the FileSystemObject

  'Remove the ASP scripting tags
  sContents = Replace (sContents, "<" & "%", "")
  sContents = Replace (sContents, "%" & ">", "")
  GetFileContentsForExecution = sContents
End Function

In our GetFileContentsForExecution() function, we first create and obtain a reference to the FileSystemObject. (For more on the FileSystemObject check out the FileSystemObject FAQ Category.)

  Dim oFSO, sContents
  'Obtain a reference to the FileSystemObject
  Set oFSO = Server.CreateObject("Scripting.FileSystemObject")

Then we do a few things in the next line:

  'Obtain the file contents
  sContents = oFSO.OpenTextFile(Server.MapPath(".") & _
                               "\" &  sTargetFile).ReadAll

Starting with our FileSystemObject reference (oFSO), we call the OpenTextFile() method passing the absolute file path to the target file as the argument. As you can see, we constructed the absolute file path on the fly using Server.MapPath("."). Since the OpenTextFile() method returns a TextStream object, we use the ReadAll method to "read all" of the contents of the file into our sContents variable.

Now that we've obtained the contents of the file, we can release the reference to the FileSystemObject (i.e., Set oFSO = Nothing 'reference to the FileSystemObject). (No sense in keeping resources, if it's not needed, right?)

It's important to note that the VBScript Execute and ExecuteGlobal statements expect only pure VBScript statements to be passed as its argument, therefore we must remove the ASP Scripting delimiters <% and %>. We'll see shortly that our file will have these delimiters as a precaution to protect our code from prying eyes.

Finally, to wrap up the GetFileContentsForExecution function, we take the file contents that we want to process and send it back as the return value of our function (GetFileContentsForExecution = sContents).

Before we move on to see how our GetFileContentsForExecution() function is used to assist in simulating Server.Execute and Server.Transfer, let's note the contents of our target file whose ASP code we want to execute:

Listing 2 (page2.asp)
  Response.Write "Our Target File is " & sTargetFile
  Response.Write "<BR>but we are processing from " & _
                Request.ServerVariables("SCRIPT_NAME") & "."

In this page, we have two Response.Write statements that will provide us information about the target file we are processing and in what context (page) we are processing it from. Notice that we've added the ASP Scripting tags <% and %>. This enables us to test and debug the page by itself by loading it directly in the browser. And if a wandering browser happened upon it, then the page would be processed vs. having all of our ASP source code available for viewing.

Now that we have our GetFileContentsForExecution function and know what script we want to execute, we need to add code to page1.asp to make the simulated Server.Execute call to the contents in page2.asp. To do this, add the following to page1.asp:

Listing 3 (added to page1.asp)
Dim sTargetFile, sTargetFileContents
sTargetFile = "page2.asp"

'Get the contents of the file to execute
sTargetFileContents = GetFileContentsForExecution(sTargetFile)
Execute sTargetFileContents

Here, we declare a variable to hold the name of the file we want to execute. Then we use our GetFileContentsForExecution() function to get the file contents. Lastly, we use VBScript's Execute statement on the sTargetFileContents that contains the target file's VBScript code. (For a quick description of Execute, see this page.)

Here's the output we get when viewing page1.asp in the browser:

Our Target File is page2.asp
but we are processing from page1.asp.

If we want to simulate a Server.Transfer, then we simply end page execution after using the Execute statement by using a Response.End statement like so:

Listing 4 (added to page1.asp to do Server.Transfer)
sTargetFile = "page2.asp"

'Get the contents of the file to execute
sTargetFileContents = GetFileContentsForExecution(sTargetFile)
Execute sTargetFileContents

Here are a few pros and cons with using this technique:


  • Simulates Server.Execute and Server.Transfer in ASP 2.0
  • Compatible with ASP 3.0
  • Easily integrated into existing code
  • Modularize code into pages
  • Run-time code evaluation
  • Provides a technique for dynamic #includes. (Read The low-down on #includes for a discussion on what dynamic includes are...)


  • Can only process pure script, however, you can output HTML using Response.Write
  • Possible performance implications due to accessing FileSystemObject for each call to page (Of course, you could implement some type of caching.)

So far, we have learned a technique that used the Execute statement to execute code contained in a separate file. This technique can be used to simulate the Server.Execute and Server.Transfer functionality available in ASP 3.0, albeit with some limitations. Although I didn't show it here, this same technique can be used to load functions, subroutines and VBScript class objects into memory as described in Dino Esposito's article (read "An Example: The Include Function" portion of his article Understanding VBScript: Runtime Code Evaluation).

Happy Programming!

  • By Francis Chau

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article