In brief, Active Server Pages (ASP) are text files that contain standard HTML commands that are processed by a Web browser, and scripting commands that are executed on a Web server. These scripting commands can be written in VBScript, JScript (the Internet Explorer equivalent of Netscape's JavaScript), or any other language for which a valid scripting engine is installed, e.g. Perl and Python. Using these scripting commands, you can introduce basic programming operations into your HTML, such as performing conditional execution and expression evaluation, as well as invoking the features of COM servers installed on the server. In this article, we'll take a look at using these scripting commands to achieve the results you want.
When an HTTP request for an ASP document is received by an ASP-enabled Web server, such as Microsoft's Internet Information Server (IIS), the server processes the scripting commands within that file. The output generated by the scripting language is combined with the standard HTML in the ASP to produce the HTTP response returned to the Web browser. In other words, the server doesn't return the ASP document to the browser. Instead, it returns static HTML, plus the output from the processing of the scripting commands. In most cases, the returned data is pure HTML, although it can potentially contain any valid MIME (Multi-Purpose Internet Mail Extensions) content type.
There are numerous benefits to using ASP:
The user can't readily access your code. Unlike with browser-side JavaScript or VBScript, in which the scripting instructions are delivered intact to the browser, ASP commands aren't sent to the Web browser. ActiveX objects accessed using ASP - when Microsoft Transaction Server (MTS) isn't being used - are loaded into the process space of the server, which reduces overhead when compared with CGI (common gateway interface) server extensions. An ASP object can also be processed in a separate process space, reducing the likelihood that a crashing ASP object will bring down your Web server. ActiveX objects used by IIS can be installed into MTS, providing activation on demand, transactions, resource sharing, crash protection, and additional security. ASP code is often easier to write than other CGI programs, in that they don't require compilation or the installation of a secondary processor (although when you create a COM server designed to be accessed by an ASP, the COM server requires compilation).
As noted already, ASP can only be used with an ASP-enabled Web server. Initially, ASP support was introduced in Microsoft's Internet Information Server (IIS). However, any Windows-based server can potentially support ASP. This is crucial in that ASP servers use COM technology, which is supported almost exclusively by the Windows operating system. (Although some non-Windows implementations of COM exist, these are quite rare.)
Before continuing, it's worth mentioning that JSP (Java Server Pages) is a technology similar to ASP. The primary difference is that JSP-enabled servers aren't limited to Windows-based servers. Any Web server that runs on a Java 2-supporting operating system (JDK 1.2) can be a potential JSP-enabled server.
Using ASP Commands
In addition to basic HTML, ASP contains four types of commands: primary scripting commands, output directives, processing directives, and include statements.
With the exception of include statements, commands contained within an ASP document are enclosed within <% %> delimiters. As mentioned earlier, these commands can consist of VBScript, JScript, or any other scripting language for which a valid scripting engine has been installed on the Web server. The language used for most of the remainder of this article is VBScript.
Primary scripting commands contain the basic syntactic elements of the scripting language being used. For example, primary scripting commands may include control structures, expression evaluation, function invocation, variable declarations, and so on. HTML commands can be interspersed between primary scripting commands to control the HTML content that is returned to the Web browser. For example, consider the following segment:
<% If Time >= #12:00:00 AM# And Time < #12:00:00 PM# Then %> Good Morning! <% Else %> Hello! <% End If %>
Here, the plain HTML text Good Morning! appears between the <%If%> and the <%Else%> commands. Alternatively, the Write method of the built-in Response object can be used to write HTML from within a primary scripting command. The Response object is one of six available built-in objects you can use in your ASP. Built-in objects are discussed in greater detail later in this section. The following is an example of ASP commands that make use of the Response.Write method:
<% If Time >= #12:00:00 AM# And Time < #12:00:00 PM# Then Response.Write "Good Morning!" Else Response.Write "Hello!" End If %>
In this example, the entire code segment is a primary scripting command. The result is that one of two strings is written to the HTML file being created by the Web server based on the evaluation of the <%If%> statement. In other words, the result of this segment will either be Good Morning! or Hello!. None of the other actual text of the primary scripting command is delivered to the browser.
Output directives. Output directives are a special case of primary scripting commands. As with the Response.Write technique, an output directive inserts text into the HTML stream that will be returned to the Web browser. Output directives use the following syntax:
<%= expression %>
The value of expression can be a variable, constant, formula, property, method, or function. For example, the following line inserts a string indicating the current time, based on the internal clock on the Web server (because the Web server can be at any location in the world, this isn't necessarily the same time as in the time zone of the Web browser!):
The current time on this server is: <%=Now%>
Processing directives. Processing directives provide instructions to the ASP processing engine and typically appear at the beginning of an ASP page. Processing directives use the following syntax:
<% @ keyword=value%>
The keyword must be a reserved word recognized by the ASP engine and must be separated from the @ sign by at least one space. The keyword is followed by an equals sign (=) and a value. Furthermore, multiple keywords can appear in a single processing directive, and no spaces can appear on either side of the equals sign. The following is an example of a processing directive that tells the Web server that the VBScript language is being used (the default):
<% @ LANGUAGE=VBScript %>
When using VBScript or JScript, the ASP processing engine removes white space. When you need to include a blank space in HTML, use the HTML non-breaking white space character, see an HTML reference for additional special character.
VBScript Comments and Variables
In VBScript, comments can appear within primary scripting commands, with the exception of output commands. Comments are defined by anything to the right of an apostrophe (with the exception of the end delimiter):
<% If Time >= #12:00:00 AM# And Time < #12:00:00 PM# Then Response.Write "Good Morning!" ' Write morning greeting. Else Response.Write "Hello!" ' It must be later in the day. End If %>
As you can imagine, comment notation depends on the scripting language. For example, while VBScript uses the apostrophe, JScript makes use of the // characters to denote a comment, similar to Java, Delphi, and C++.
Although variables don't need to be declared in VBScript, they should be declared for good programming practice. Once declared, or assigned in those cases in which they aren't declared, variables are accessible to the remaining commands in that ASP page. For example, the following statement declares a variable named UserName, and sets its value to that of a value passed in the HTTP query string. The following variable can be used in any expression that appears in any commands later in this page:
<% Dim UserName Set UserName = Response.QueryString("Name") %>
There are two special classes of variables that are visible to more than one ASP page. These are Session and Application variables. Session variables are available to all pages accessed by a particular user within a session. By comparison, Application variables are shared by all ASP pages in an ASP application. These variables are stored using the Session and Application built-in objects, respectfully. These objects are described in greater detail later in this article.
You access Session and Application variables by passing the name of the variable to the Session or Application object. For example, the following code stores the session start time in a Session variable:
<% Session("SessionStart") = Now %>
All pages on the site accessed by a specific user can then read this value using the following statement:
You began your session at <% = Session("SessionStart") %>
VBScript doesn't permit the declaration of constants. Constants are defined using type libraries.
Include Files
Include files are similar to Delphi's Web Broker TPageProducer in that they permit you to insert segments of HTML into your page at specific points. For example, if you have a segment of HTML that should appear at the top of every page, you can create it once and embed it within each ASP page by using the include directive before any other simple HTML text or commands on the page.
The include statement has the following syntax:
If the keyword VIRTUAL is used, the dirname part is a virtual directory (defined through server configuration). If FILE is used, the directory can either be an absolute or a relative directory. For example, if you have created a virtual directory under IIS named Chunks, you can use the following include statement to include the header.htm HTML in your ASP page:
In addition to including static HTML, include files can also reference ASP pages or ASP segments. However, note that ASP segments referenced by include directives are processed on the server before the referencing ASP page is processed. This processing is unconditional. That is, all included ASP pages are processed - even those where the include statement is referenced within an <%IF%> statement - before the first primary command of the referencing ASP page is processed.
Using Objects
ASP objects are Automation servers, and therefore can be either in-process servers or out-of-process servers. There are two general classes of objects that you can use from an ASP: built-in objects and custom objects.
The Server and Application objects referenced previously are examples of built-in objects. These objects are available to all ASP pages. These built-in objects are automatically created for you by the server, and can be referenced within any primary commands. By comparison, you must explicitly create custom objects before you can access them. You create custom objects by calling the CreateObject method of the Server built-in object, passing to it the PROGID of the registered object. For example, if you have registered an Automation server with the PROGID of Project1.Text, you can create an instance of it, and assign this instance to the variable DelphiASPObj using the following command:
<% Set DelphiASPObj = Server.CreateObject("Project1.Test") %>