How ASP Works

Active Server Pages is implemented as an IIS componentin fact, as an ISAPI filterthat resides in a dynamic link library named ASP.DLL. (ISAPI filters are custom web server extensions that are called for every HTTP request received by the web server.) If the file extension of the resource requested by the client is .asp, Active Server Pages is used to parse the file and handle the client request; otherwise, it is bypassed.

ASP does not view pages purely on a one-by-one basis. Instead, it organizes its pages into applications. AnASP application is the entire set of files that can be accessed in a virtual directory and its subdirectories. This notion of an ASP application allows you to define global variables whose values are shared across all users of your ASP application, as well as to have ASP save state information from a particular client's session.

When ASP is used to parse a web page, it first checks to see whether the request has originated from a new client. If the client is new, ASP checks the global.asa file (which is stored in the application's virtual root directory) to determine whether any session-level data is to be initialized. If the client's is the first request for the ASP application, ASP also checks global.asa for any application-level data as well. ASP then parses the HTML page, executes any script contained on the page, and includes any output from scripts into the HTML stream. Note that the output of ASP is HTML with or without client-side script; no server-side script contained in the ASP page is passed on to the client.

5.1.1 The global.asa File

As we've noted, when ASP receives a request from a new user, it checks the global.asa file, which must be located in the ASP application's virtual root directory. If the request from the new user is the first request for the ASP application, theApplication_OnStart event procedure, if it is present, is executed before theSession_OnStart event. When a user session ends, usually because the session has timed out, ASP checks global.asa for aSession_OnEnd event. When the application's last user session ends, ASP also checks whether code for the Applictaion_OnEnd event is present as well.

In addition, global.asa can use the tag to define application-level and session-level objects. All objects declared to have application scope with the tag are available throughout the application and can be accessed through the Application object's StaticObjects collection. tagged objects that have session scope are available in a single client session and can be accessed through the Session object's StaticObjects collection. The syntax of the tag is:

scope ID=name PROGID=progid>

where scope is either Application or Session, name is the name by which the object variable will be referenced in code, and progid is the object's programmatic identifier, as defined in the registry.

Example 5-1 shows the shell of a simple global.asa file. This file is fully customizable, so it can be changed to cater to your specific application needs. Of course, event handlers that you don't intend to use need not be present in the file. If you choose not to take advantage of any application-level or session-level variables, initialization, and cleanup, you need not create a global.asa file.

Example 5-1. The structure of a global.asa file

strName PROG>strName 
 PROG>

5.1.2 Including Server-Side Script in Web Pages

ASP offers two methods for incorporating server-side script into a web page: server-side includes and the HTML tags define a single code block. The

RUNAT= Server

A common source of error for those writing their own ASP code is the omission of the RUNAT attribute. This causes the code to execute on the client rather than on the server, which invariably produces numerous syntax and other errors.

A single web page can include any number of tags. The tags can be located anywhere within the

...

or the

tags of an ASP document. A single script block must contain code written in a single language to run at a single location (i.e., either the server or the client). If you want to run code on both the server and the client, separate script blocks are required. In the latter case, you can omit the RUNAT attribute from the tags to complete functions and procedures, as well as to variable declarations (but not assignments) using the Dim, Public, and Private statements.

The second way to include script in an HTML page is to use the <%...%> or <%= %> delimiters, or the primary script commands. All code within the delimiters must be written in the primary scripting language defined for the application or for the ASP page, whichever has the least restrictive scope. The default application scripting language is defined by theDefault ASP Language property on the App Options tab in the snap-in for IIS 5.0. It can be overridden for an individual page by including the <@ LANGUAGE=ScriptingEngine%> directive at the beginning of an ASP page, where ScriptingEngine is the name of the language.

Both types of primary script commands contain code that is executed sequentially as the portion of the HTML page that contains them is parsed. The difference between the <%...%> and the <%=...%> delimiters is that the former can contain executable code but does not automatically send output to the HTML response stream, while the latter contains a variable or expression whose value is output into the HTML response stream. In practice, this is not a restriction for the former tag, since you can use the Response.Write method from the ASP object model to write to the HTML output stream.

Example 5-2 shows a simple ASP page that contains both a script block and primary script commands. The first primary script command calls the user-defined Greeting function and writes the string it returns to the HTML response stream. The Greeting function itself is defined in the script block. It retrieves the time on the server and returns a string indicating whether it is morning, afternoon, or evening. The second primary script command simply calls the VBScript Now function to insert the date and time into the HTML response stream. Notice from the HTML source shown in Figure 5-1 that the HTML page produced by the ASP page in Example 5-2 contains HTML only; the server-side script has been either discarded or replaced with the text that it has output.

Example 5-2. A simple ASP page


 

A Simple ASP Page <%=Greeting( ) %>, the time is <%= Now %> on the server.

Figure 5-1. HTML source produced by the ASP page in Example 5-2

figs/vbs2.0501.gif

Note that ASP allows you to import script or HTML from external files by using the #include server-side directive. It is discussed in Chapter 3.

5 2 Active Server Pages Object Model

Part I: The Basics

Introduction

Program Structure

Data Types and Variables

Error Handling and Debugging

VBScript with Active Server Pages

Programming Outlook Forms

Windows Script Host 5.6

VBScript with Internet Explorer

Windows Script Components

Part II: Reference

Part III: Appendixes

Appendix A. Language Elements by Category

Appendix B. VBScript Constants

Appendix C. Operators

Appendix E. The Script Encoder



Vbscript in a Nutshell
VBScript in a Nutshell, 2nd Edition
ISBN: 0596004885
EAN: 2147483647
Year: 2003
Pages: 335

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net