Professional Documents
Culture Documents
Microsoft Corporation
Updated December 27, 2000
http://msdn.microsoft.com/workshop/server/asp/asptutorial.asp
Summary: This two-module tutorial provides a step-by-step introduction to several ASP features.
The tutorial shows how to build sample applications and demonstrates key concepts youll need to
understand when developing your own ASP pages. (11 printed pages)
Welcome to the Active Server Pages (ASP) tutorial. If you are new to Microsoft Internet
Information Services (IIS), this introduction provides necessary information about tutorial directory
structure and permission issues. The two tutorial modules show you how to build sample
applications, while demonstrating key concepts youll need to understand when developing your own
ASP pages. We suggest you work through each lesson in Module One before starting Module Two.
Introduction. This section defines what ASP is, how it works, and other technologies that can
be used in conjunction with ASP to create active content on your Web site. It also describes the
directory structure you should use for the tutorial and security considerations that may affect your
ability to run the tutorial.
Module One. This module introduces the creation of an ASP page, and focuses on the
collection and transfer of data. It contains the following lessons:
Write and Run an ASP Page. Describes how to use Visual Basic Scripting Edition
(VBScript) and HTML tags.
Send Information by Using Forms. Shows how to use forms to send information.
Create a Guest Book. Shows how to create a sign-in book on your ASP page for
visitors to your Web site.
Rotate Information Randomly. Shows how to rotate ads on your Web page randomly.
Record Traffic with a Page Counter. Demonstrates the use of a page counter to track
the number of hits a page receives.
Introduction
Microsoft Active Server Pages (ASP) is a server-side scripting technology that can be used to create
dynamic and interactive Web applications. An ASP page is an HTML page that contains server-side
scripts that are processed by the Web server before being sent to the users browser. You can
combine ASP with Extensible Markup Language (XML), Component Object Model (COM), and
Hypertext Markup Language (HTML) to create powerful interactive Web sites.
Server-side scripts run when a browser requests an .asp file from the Web server. ASP is called by
the Web server, which processes the requested file from top to bottom and executes any script
commands. It then formats a standard Web page and sends it to the browser.
It is possible to extend your ASP scripts using COM components and XML. COM extends your
scripting capabilities by providing a compact, reusable, and secure means of gaining access to
information. You can call components from any script or programming language that supports
Automation. XML is a meta-markup language that provides a format to describe structured data by
using a set of tags.
As you work through the lessons in each module, save your work in the
C:\Inetpub\Wwwroot\Tutorial directory to view the content on your Web site. If you did not
customize your installation of IIS, C:\Inetpub\Wwwroot was created as the default home directory.
To load and run the tutorial samples, you must have administrator privileges on the computer
running IIS. The default IIS security settings should allow you to run the tutorial, but you may need
to change your security settings on the directory if you encounter access violations. See the product
documentation for more information about IIS security settings.
Module One
This module explains how to create ASP pages (.asp files) that use the fundamental features of
HTML, ASP, and VBScript. This module includes the following lessons:
Write and Run an ASP Page. Describes how to use Visual Basic Scripting Edition (VBScript)
and HTML tags.
Send Information by Using Forms. Shows how to display forms on an HTML page.
Create a Guest Book. Uses forms to gather information from visitors, store the information in
a database, and display the database contents in a Web page.
Display an Excel Spreadsheet in ASP. Explains how to display an Excel spreadsheet in a Web
page.
<example>
In VBScript, you use the percent sign with brackets as delimiters around the tags:
<%example%>
You can put many tags inside one pair of VBScript delimiters:
<%example, samples%>
Example 1
This example displays the words "Hello World". To run this example, cut and paste it into an empty
file and save it in the C:\Inetpub\Wwwroot\Tutorial directory as Example1.asp. Make sure to save
your file with an .asp extension. Some text editors automatically change the file name extension to
.txt when the Text Document option is selected in the Save dialog box. To prevent this, select the
All Files(*.*) option. Exit your text editor as the server may not be able to display an HTML page
that is open in a text editor. View your page with your browser by typing http://<Your Server
Name>/Tutorial/Example1.asp in the address bar.
When running Example 1, the page is processed by the Web server in the following sequence:
1.
2.
3.
4.
5.
6.
<html>
7.
<head>
8.
<title>Example 1</title>
9.
</head>
10.
<body>
11.
<%
12.
13.
%>
14.
<%=FirstVar%>
15.
</body>
</html>
Example 2
This example incorporates a FOR loop in the ASP page. The FOR loop is a statement that prints
"Hello World" 10 times. To create Example 2, use the file from Example 1 and add the FOR loop code
as shown in the following code sample. Save the file as Example2.asp. View it in your browser.
Button Example
In this example, there are three input lines that use "buttons," and two default buttonsRESET and
SUBMIT. The Post method is used to send data from the client browser to the Web server. Open your
text editor, create a new file, and paste in the following code. Save the file as Button.htm and view
the page in your browser.
<html>
<head>
<title>Button Form</title>
</head>
<body>
<FORM NAME="Button Example" METHOD="POST" ACTION="tutorial/button.htm">
Computer Programming Experience:
<P>
<INPUT TYPE="button" NAME="choice" VALUE="Less than 1">Less than 1
year.<BR>
<INPUT TYPE="button" NAME="choice" VALUE="1 to 5">1-5 years.<BR>
<INPUT TYPE="button" NAME="choice" VALUE="More than 5">More than 5 years.
</P>
<P><INPUT TYPE="reset" VALUE="Clear Form">
<INPUT TYPE="submit" VALUE="Submit">
</P>
</form>
</body>
</html>
Text example
In this example, you create text fields in a form. Open a new file in your text editor, paste in the
following code, and save the file as Text.htm:
<html>
<head>
<title>Text Form</title>
</head>
<body>
<FORM NAME="Text Example" FORM METHOD="POST" ACTION="tutorial/text.htm">
<TABLE>
<TR>
<TD ALIGN="RIGHT" VALIGN="MIDDLE">Name?
<TD ALIGN="LEFT">
<INPUT TYPE="text" NAME="name" VALUE=""
SIZE="20" MAXLENGTH="150">
<TR>
<TD ALIGN="RIGHT" VALIGN="MIDDLE">Company?
<TD ALIGN="LEFT">
<INPUT TYPE="text" NAME="company" VALUE=""
SIZE="25" MAXLENGTH="150">
<TR>
<TD ALIGN="RIGHT" VALIGN="MIDDLE">
Email Address?
<TD ALIGN="LEFT">
<INPUT TYPE="text" NAME="email" VALUE=""
SIZE="25" MAXLENGHT="150">
</TABLE>
<INPUT TYPE="reset">
<INPUT TYPE="Submit" NAME="Submit" VALUE="Submit">
</form>
</body>
</html>
Create a Guest Book
This lesson describes how to develop a guest book application. Guest books allow visitors to your site
a chance to give you feedback. Information such as the visitors name, e-mail address, and
comments can be available to you.
Creating the Guest Book Database
You must first create an Access database called Guestbook.mdb and save it in the
C:\Inetpub\Wwwroot\Tutorial directory. The database must have the fields with the properties
described in the following table.
Field Name
ID
TB1
TB2
TB3
TB4
MB1
After you create the database, you need to create a data source name (DSN) connection to the
database so your ASP application can interact with it. The DSN must be created on the Web server
that is running the ASP application. If you move the ASP application to a different Web server, you
have to re-create the DSN on the new Web server. The following procedure describes how to create a
DSN on Windows NT and Windows 2000.
1.
2.
3.
4.
5.
6.
Now that you have created the database and the DSN, paste the following code into a file named
Guestbook.asp and place it in the C:\Inetpub\Wwwroot\Tutorial directory.
ELSE%>
<h1>Guestbook</h1>
<!--Post information to Guestbook form -->
<form name=guestbook.asp
action="guestbook.asp"
method="POST">
<p>To</p>
<p><input type="Text" name="To"></p>
<p>Email Address</p>
<p><input type="Text" name="EmailAdd"></p>
<p> CC</p>
<p><input type="Text" name="CC"></p>
<p>Subject</p>
<p><input type="Text" name="Subject"></p>
<p>Message</p>
<p><textarea name="Memo" rows=6 cols=70></textarea></p>
<input
<h1>Guest Book</h1>
<form name=viewdb.asp action=viewdb.asp method=post>
<table border=1 cellspacing=3 cellpadding=3 rules=box>
<%
ON ERROR RESUME NEXT
IF rst.EOF THEN
Response.Write "There are no entries in the database."
ELSE%>
<tr>
<%
Deletes rows from the database, this cannot be undone
Response.Write "<td width=200><center>Delete Record</center></td>"
FOR i = 1 to rst.Fields.Count -1
Response.Write "<td width=200><input name=sort value=" & rst(i).Name & "
type=submit></td>"
NEXT
WHILE NOT rst.EOF %>
<tr>
<%
Response.Write "<td align=left valign=top bgcolor=#ffffff><a
href=viewdb.asp?id=" & rst(0) & ">Delete</a></td>"
FOR i = 1 to rst.fields.count - 1
Response.Write "<td align=left valign=top bgcolor=#ffffff>" & rst(i)
&"</td>"
NEXT
rst.MoveNext
WEND
END IF
%>
</table>
</form>
</body>
</html>
2.
3.
4.
5.
Create a spreadsheet using either Excel 98 or Excel 2000 and save it as ASPTOC.xls in the
C:\Inetpub\Wwwroot\ directory.
Note: Do not include any special formatting or column labels when creating the spreadsheet.
Highlight the rows and columns on the spreadsheet that you want displayed in the Web page.
On the Insert menu, choose Name, and select Define.
If there are any names listed, select them and select Delete.
Type a name for the workbook, select Add, and select OK.
To display the spreadsheet in a Web page, you must create a DSN for the spreadsheet. The process
is the same as it was for the database lesson earlier in this module. The only difference is that you
must select the {Microsoft Excel Driver (*.xls)} option for the DSN.
Once you have created the spreadsheet and named the table in the spreadsheet, and also created a
DSN for the spreadsheet, you are ready to create the page that displays the contents. Paste the
following code into a new file and name it ASPTOC.asp. Save it in the C:\Inetpub\Wwwroot\Tutorial
directory and view the page in your browser.
<%@Language=VBScript %>
<html>
<head>
<title> Displaying An Excel Spreadsheet in an Web Page </title>
</head>
<body bgcolor="#FFFFFF" text="#000000" >
<h1>ASP Table of Contents</h1>
<%
Creates an instance of an Active Server Component
Set oConn = Server.CreateObject("ADODB.Connection")
Connects to the Excel driver and the Excel spreadsheet
in the directory where the spreadsheet was saved
strConn = "Driver={Microsoft Excel Driver (*.xls)};
DBQ=C:\Inetpub\Wwwroot\Tutorial\ASPTOC.xls;"
Module Two
This module explains how to develop an ASP page that delivers services useful in e-commerce. This
module includes the following lessons:
Redirect Users from Ad Links. Redirect browsers to advertisers sites when users click on an
ad image.
Count Page Hits. Track the number of times users request a page.
Rotate Ad Information
Advertising is big business on the Web. This lesson explains how to take advantage of the Ad Rotator
component installed with ASP by describing how to use this component to rotate advertisements on
your Web pages. The Ad Rotator component selects an advertisement for your Web page each time
the user refreshes or loads the Web page. Two files are required to set up the Ad Rotator component:
an Ad Rotator Include file and an ad images data file. By setting up these two files, this component
can be called by any ASP page on your site. Changes to the ad parameters are not done on all the
sites containing the ad, but to the ad images data file. This saves lots of time if the ad appears on
numerous pages within your Web site.
Write an Ad Rotator Include File. Creates the ad-image links on any page that calls this file.
Create an Ad Image Data File. Specifies global ad-display data and information specific to
each ad.
Test the Ad Rotator. Uses an ASP page that calls the Ad Rotator logic Include file and the
image data file to display and rotate ads.
Write an Ad Rotator Include file
Include files are used to store information that will be used by more than one ASP or HTML file. By
creating an Ad Rotator Include file, when changes need to be made to specific information, you only
need to change the information in one location. This lesson will guide you in creating an Ad Rotator
Include file containing a function named getAd(). This function randomly selects ads to display on
your ASP pages. You can call this file from within any ASP page intended to display the rotated ads.
When you test calling the Include file from an ASP page (see Test the Ad Rotator), you will use some
images from Microsoft.com for ad images.
Open a new file in your text editor, paste in the following script, and save the file as
Adrotatorlogic.inc:
<%
Function getAd()
Dim load
getAd = load.GetAdvertisement("adimagedata.txt")
End Function
%>
The data file is divided into two sections that are separated by an asterisk (*). The first section
provides information common to all the ads to be displayed. The second section lists data relevant to
each ad.
REDIRECTION. URL, the path and name of the ASP file that redirects browsers that
select ad images.
AdURL. Virtual path and filename of the image file containing the advertisement.
AdHomeURL. URL to jump to when this link is selected. To indicate there is no link,
use a hyphen.
Open a new file in your text editor, paste in the following script, and save the file as
Adimagedata.txt:
REDIRECT adrotatorredirect.asp
WIDTH 250
HEIGHT 60
BORDER 0
*
images/windows_logo.gif
http://www.microsoft.com/windows
Microsoft Windows
2
images/office_logo.gif
http://www.microsoft.com/office
Office 2000
3
Test the Ad Rotator
To test the system, you will need an ASP page that calls the Ad Rotator Include file and the ad
images data file, and then displays the ads. First, you will need test ad images stored on your site.
2.
3.
Open a new file in your text editor, paste in the following script, and save the file as Displayad.asp:
<li type="disc">After you add the hit counter script to this page, under
"Statistics" you should see the number of page hits since the script was
added.</li>
</ul>
<br><br><br><br><br>
<h3>Statistics</h3>
<% Set pageCount = Server.CreateObject("MSWC.PageCounter") %>
<!--Increment the counter-->
<% pageCount.PageHit %>
<p>You are visitor number <% =pageCount.Hits %> to this Web page.</p>
</body>
</html>
Redirect Users from Ad Links
When a user clicks the ad, the browser appends a query string to the request to the server. Then,
the server directs the users browser to the ads URL.
Open a new file in your text editor, paste in the following script, and save the file as
Adrotatorredirect.asp:
<%@Language=VBScript %>
<html>
<head>
<title>Redirection Page</title>
</head>
<body>
<%
'Set the response buffer on
Response.Buffer = True
Dim lsURL
'Obtain the URL from the query string
lsURL = Request.QueryString("URL")
'Clear the response and redirect to URL
Response.Clear()
Response.Redirect(lsURL)
%>
</body>
</html>
To check your work, use Displayad.asp. When you click on an ad, you should see a new window
displaying an appropriate ad-related Web page.
The Page Counter component uses an internal object to record page hit-count totals for all pages on
the server. At regular intervals, this object saves all information to a text file so that no counts are
lost due to power loss or system failure. The Page Counter component uses the following three
methods:
Hits(). This displays the number of hits for a Web page. The default is the current page.
PageHit(). This increments the hit count for the current page.
Reset(). This resets the hit count for a page to zero. The default is the current page.
An in-use sample of this script can be seen at the bottom of the Displayad.asp script. To place a page
hit counter on a Web page, place the following script where you want the counter displayed on the
page: