How and when to use Server.Execute and Server.Transfer
The Server-side
include was used to insert code from another page into the current one. All server-side
include are carried out before any. This means that if you want to use your ASP
code to determine what file to include, you are in trouble. This is why you might
want to use Server.Execute.
Server.Execute
(path) execute the ASP script specified by path. If path is an absolute path,
it should be for an ASP page within the same application space (the same folder
or one of its subfolder) path may contain query string date.
Suppose
for a moment that you want some information at the top and bottom of every page
on your site. This might be some graphics, links, contact information, and so
on. One way you could do this is to include some file at the top of every page
and another one at the bottom. This will work, but it lacks some flexibility.
You might decide that you want two different ways to view each page. If you want
to keep your options open for expansion, you might decide to use server. Execute.
Transferring
Control to another Page
Server.Transfer
is used to transfer control to another ASP page. When it is called, all the data
related to the calling page is transferred to the new page. This means that if
variables of session or application scope has been given values, those values
are kept in the new page. State information and values for the built-in objects
are transferred, too. Also the contents of the request collections are kept and
are available to the new page.
You
can even perform a server.Transfer between two pages in separate applications.
In this case, the value of the application variables and objects will be the same
as if the second page were in the same application as the first. That is, the
values of application scope variables and objects are kept after the transfer.
Demonstrates
How to Use Server.Execute
<%@
LANGUAGE=VBSCRIPT %> <% Option Explicit %> <HTML> <BODY>
<% Response.Write( I am in page 1 <BR>) Server.Transfer(page2.asp)
Response.Write(Back in page 1) %> < / BODY> </HTML>
This page is almost identical to page1.asp. the only difference is that line
7 uses a server. Transfer in place of the server.Execute. You can see the result
of this listing. Notice that in this version, the third line is not printed. That
is because you never return to the calling page when you do a server.Transfer.
In this regard, server.Transfer may be used a bit like the Response. Redirect.
Redirect
tells the users browser to make a new request. This result in the creation
of a new object context, which is used to contain the session and request objects
as well as some server variables. Sometime when you want to send the user to different
page, you will want a new object context to be created. In such case, you should
use Response.Redirect.
If you do not need a new object context, you are
probably better off using server.Transfer, which is faster because it dose not
involve as many communications or the creation of a new object context.