Creating ASP Pages

An Active Server Pages (ASP) file is a text file with the extension .asp that contains any combination of the following:

It’s easy to create an .asp file. For any HTML file to which you want to add scripts, rename the file by replacing the existing .htm or .html file name extension with .asp. To make the .asp file available to Web users, save the new file in a directory on your Web site (be sure that the directory has Script or Execute permission enabled). When you view the file with your browser, you see that ASP processes and returns an HTML page. You can now add script commands to the .asp file.

Note   Because .asp files require extra processing, you should not convert all of your HTML pages into ASP pages. Convert only the files that will have script commands into .asp files. You can put both .asp and .htm files in the same directory.

You can use any text editor to create .asp files. You may find it more productive to use an editor with enhanced support for ASP, such as Microsoft® Visual InterDev™. If you have never used HTML before, consider getting started with Microsoft FrontPage. FrontPage lets you create documents and format text just as you would with a word processor. You can then add simple ASP commands to the HTML page created by FrontPage by using the Insert Script command.

Adding Script Commands

A script is a series of commands or instructions. Unlike HTML tags, which simply format text or read in a graphic, video, or sound file, a script command instructs the Web server to perform an action. Script commands can store a user’s name in a variable, display the user’s name in a page returned to the browser, or store the user’s name in a database.

Script commands are differentiated from text by delimiters. A delimiter is a character or sequence of characters that marks the beginning or end of a unit. In the case of HTML, these delimiters are the less than (<) and greater than (>) symbols, which enclose HTML tags.

ASP uses the delimiters <% and %> to enclose script commands. You can include within the delimiters any command that is valid for the scripting language you are using. The following example shows a simple HTML page that contains a script command:

<HTML>
<BODY>
This page was last refreshed on <%= Now %>.
</BODY>
</HTML>

The VBScript function Now returns the current date and time. When the Web server processes this page, it replaces <%= Now %> with the current date and time and returns the page to the browser:

This page was last refreshed on 8/1/97 2:20:00 PM.

Commands enclosed by delimiters are called primary script commands. These commands are processed using the primary scripting language. Any command that you use within script delimiters must be valid for the primary scripting language. By default, the primary scripting language is VBScript. You can set a different language. See Working with Scripting Languages.

If you are already familiar with client-side scripting (writing scripts that run on the browser), note that you do not use the HTML <SCRIPT> element to enclose expressions. For server-side scripting, you only use the <SCRIPT> element to define procedures in languages other than the primary scripting language. For more information, see Working with Scripting Languages.

Mixing HTML and Script Commands

You can include within ASP delimiters any statement, expression, procedure, or operator that is valid for your primary scripting language. A statement, in VBScript and other scripting languages, is a syntactically complete unit that expresses one kind of action, declaration, or definition. The conditional If...Then...Else statement that appears below is a common VBScript statement.

<% 
If Time >= #12:00:00 AM# And Time < #12:00:00 PM#  Then 
  Greeting = "Good Morning!" 
Else 
  Greeting = "Hello!" 
End If
%> 

<%= Greeting %>

This statement stores either the value "Good Morning!" or the value "Hello!" in the variable greeting. The <%= Greeting %> command sends the current value of the variable to the browser.

Thus, a user viewing this script before 12:00 noon (in the Web server’s time zone) would see this line of text:

Good Morning!

A user viewing the script at or after 12:00 noon would see this line of text:

Hello!

You can include HTML text between the sections of a statement. For example, the following script, which mixes HTML within an If...Then...Else statement, produces the same result as the script in the previous example:

<% If Time >= #12:00:00 AM# And Time < #12:00:00 PM# Then %> 
Good Morning!
<% Else %>
Hello!
<% End If %>

If the condition is true—that is, if the time is midnight or after, and before noon—then the Web server sends the HTML that follows the condition (“Good Morning”) to the browser; otherwise, it sends the HTML that follows Else (“Hello!”) to the browser. This way of mixing HTML and script commands is convenient for wrapping the If...Then...Else statement around several lines of HTML text. The previous example is more useful if you want to display a greeting in several places on your Web page. You can set the value of the variable once and then display it repeatedly.

Rather than interspersing HTML text with script commands, you can return HTML text to the browser from within a script command. To return text to the browser, use the ASP built-in object Response. The following example produces the same result as the previous scripts:

<% 
If Time >= #12:00:00 AM# And Time < #12:00:00 PM#  Then 
  Response.Write "Good Morning!"
Else
  Response.Write "Hello!"
End If 
%>

Response.Write sends the text that follows it to the browser. Use Response.Write from within a statement when you want to dynamically construct the text returned to the browser. For example, you might want to build a string that contains the values of several variables. You will learn more about the Response object, and objects in general, in Using Components and Objects and Sending Content to the Browser. For now, simply note that you have several ways to insert script commands into an HTML page.

You can include procedures written in your default primary scripting language within ASP delimiters. Refer to Working with Scripting Languages for more information.

If you are working with JScript commands, you can insert the curly braces that indicate a block of statements directly into your ASP commands even if they are interspersed with HTML tags and text. For example:

<% if (screenresolution == "low") { %>
This is the text version of a page.
<% } else { %>
This is the multimedia version of a page.
<% } %>

Using ASP Directives

ASP provides directives that are not part of the scripting language you use. These directives are the output directive and the processing directives.

The ASP output directive <%= expression %> displays the value of an expression. This output directive is equivalent to using Response.Write to display information. For example, the output expression <%= sport %> sends the word climbing (the current value of the variable) to the browser.

The ASP processing directive <%@ keyword %> gives ASP information it needs to process an .asp file. For example, the following directive sets VBScript as the primary scripting language for the page:

<%@ LANGUAGE=VBScript %>

The processing directive must appear on the first line of an .asp file. Do not put the processing directive in a file included with the #include statement. You must use a space between the at sign (@) and the keyword. The processing directive has the following keywords:

You can include more than one keyword in a single directive; keyword/value pairs must be separated by a space. Do not put spaces around the equal sign (=). The following example sets both the scripting language and the code page:

<%@ LANGUAGE=JScript CODEPAGE=932 %>

White Space in Scripts

If your primary scripting language is either VBScript or JScript, ASP removes white space from commands. For all other scripting languages, ASP preserves white space so that languages that depend on position or indentation, such as Python, are correctly interpreted. White space includes spaces, tabs, returns, and line feeds.

For VBScript and JScript, you can use white space after the opening delimiter and before the closing delimiter to make commands easier to read. The following commands are all valid:

<% Color = "Green" %>

<%Color="Green"%>

<%
Color = "Green"
%>

ASP strips out white space between the closing delimiter of a statement and the opening delimiter of the following statement. If you need to preserve the white space between two statements, such as when you are displaying the values of variables in a sentence, use an HTML nonbreaking space character (&nbsp;). For example:

<%
'Define two variables with string values.
Hello = "Hello"
World = "World"
%>

<P>My response is to say "<%= Hello %>&nbsp;<%= World %>." </P>