Active Server
Pages (ASP) Basics:
An Active Server Page (ASP) file is a text file with the extension .asp
that contains any combination of the following:
Text
HTML tags
Server-Side scripts
A quick way to create an .asp file is to rename your HTML files by replacing
the existing file extension (.htm or .html) with .asp. If your file
does not contain any ASP code, then the server simply sends the file
to the user's browser and it is displayed the same as your .htm or .html
file. As a Web developer, this provides you with great flexibility because
you can assign your files .asp extensions, even if you haven't added
ASP functionality yet. Once you do add ASP code to that page, you won't
have to rename the page and change all of the links to that page in
the entire site.
To publish an .asp
file, save a new file in your WWW folder, or in a virtual directory
within WWW on your Web site. Next, request the file with your browser
by typing in the file's exact URL. After the file loads, you will notice
that the server has returned an HTML page. This may seem strange at
first, but remember that the server executes all ASP server-side scripts
prior to sending the file to the browser. The user will always
receive standard HTML.
You can use any
text editor to create .asp files. As you progress in your use of ASP,
you may find it more practical to use an editor with enhanced support
for ASP, such as Macromedia Dreamweaver UltraDev, or Microsoft®
Visual InterDev.
Adding Server-Side
Script Commands
A server-side script is a series of instructions used to issue commands
to the Web server. 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.
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, but you can also set a different default language.
* See Working
with Scripting Languages (Microsoft Support Site).
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.
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:
<%
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 time zone that the web server
resides) 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 true - that is, if the time is 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 mingling
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.
* 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.")
}
%>
Using ASP Directives
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. You must use a space between the at sign
(@) and the keyword. The processing directive has the following keywords:
- The LANGUAGE
keyword sets the scripting language for the .asp file.
- The ENABLESESSIONSTATE
keyword specifies whether an .asp file uses session state.
- The CODEPAGE
keyword sets the code page (the character encoding) for the .asp file.
- The LCID keyword
sets the locale identifier for the file.
The TRANSACTION keyword specifies that the .asp file will run under
a transaction context.
* 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" %>
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 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>