An Active Server Pages (ASP) file is a text file with the extension .asp that contains any combination of the following:
A quick way to create an .asp file is to rename your HTML files by replacing the existing .htm or .html file name extension with an .asp extension. If your file does not contain any ASP functionality, then the server dispenses with the ASP script processing and efficiently sends the file to the client. As a Web developer, this affords you tremendous flexibility because you can assign your files .asp extensions, even if you do not plan on adding ASP functionality until later.
To publish an .asp file on the Web, save the new file in a virtual directory on your Web site (be sure that the directory has Script or Execute permission enabled). Next, request the file with your browser by typing in the file's URL. (Remember, ASP pages must be served, so you cannot request an .asp file by typing in its physical path.) After the file loads in your browser, you will notice that the server has returned an HTML page. This may seem strange at first, but remember that the server parses and executes all ASP server-side scripts prior to sending the file. The user will always receive standard HTML.
You can use any text editor to create .asp files. As you progress, you may find it more productive to use an editor with enhanced support for ASP, such as Microsoft® Visual InterDev. (For more information, visit the Microsoft Visual InterDev Web site at http://msdn.microsoft.com/vinterdev/.)
A server-side script is a series of instructions used to sequentially issue commands to the Web server. (If you have developed Web sites previously, then you are probably familiar with client-side scripts, which run on the Web browser.) In .asp files, scripts are differentiated from text and HTML 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. Within the delimiters, you can include 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 with the following result:
This page was last refreshed on 01/29/99 2:20:00 PM.
Commands enclosed by delimiters are called primary script commands, which 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, but you can also set a different default language. See Working with Scripting Languages.
If you are already familiar with client-side scripting, you are aware that the HTML <SCRIPT> tag is used to enclose script commands and expressions. You can also use the <SCRIPT> tag for server-side scripting, whenever you need to define procedures in multiple languages within an .asp file. For more information, see Working with Scripting Languages.
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:
<% Dim dtmHour dtmHour = Hour(Now()) If dtmHour < 12 Then strGreeting = "Good Morning!" Else strGreeting = "Hello!" End If %> <%= strGreeting %>
Depending on the hour, this script assigns either the value "Good Morning!"
or the value "Hello!"
to the string variable strGreeting
. The <%= strGreeting %>
statement sends the current value of the variable to the browser.
Thus, a user viewing this script before 12:00 noon (in the Web servers 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:
<% Dim dtmHour dtmHour = Hour(Now()) If dtmHour < 12 Then %> Good Morning! <% Else %> Hello! <% End If %>
If the condition is truethat is, if the time is before noonthen 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:
<% Dim dtmHour dtmHour = Hour(Now()) If dtmHour < 12 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, which 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. <% } %>
--Or--
<% if (screenresolution == "low") { Response.Write("This is the text version of a page.") } else { Response.Write("This is the multimedia version of a page.") } %>
ASP provides directives that are not part of the scripting language you use: the output directive and the processing directive.
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 <%= city %>
displays the word Baltimore
(the current value of the variable) on the browser.
The ASP processing directive <%@ keyword %>
gives ASP the 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. To add more than one directive to a page, the directive must be within the same delimiter. Do not put the processing directive in a file included with the #include statement. (For more information, see Including Files.) You must use a space between the at sign (@) and the keyword. The processing directive has the following keywords:
Important 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" %>
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 dependent upon position or indentation 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. All of the following statements are valid:
<% Color = "Green" %> <%Color="Green"%> <% Color = "Green" %>
ASP removes white space between the closing delimiter of a statement and the opening delimiter of the following statement. However, it is good practice to use spaces to improve readability. 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 ( ). For example:
<% 'Define two variables with string values. strFirstName = "Jeff" strLastName = "Smith" %> <P>This Web page is customized for "<%= strFirstName %> <%= strLastName %>." </P>