Last updated: 10/6/00
Link back to course Welcome
![]() |
Using Active Server Pages |
Programming
Programming can seem intimidating, and Active Server Pages does involve programming. Here are some reasons why programming can seem intimidating.
Introduction and Background: the Big Picture
ASP or Active Server Pages is a method of processing data collected using an HTML form. The processing is server-side (occurring on the server) rather than client-side (occuring on the client). The are other server-side systems, such as PHP, Cold Fusion and CGI. There are also client-side methods, such as Javascript. Processing is frequently done by means of a script or list of instructions, like a computer program. Like processing, scripting can be client-side or server-side.
| Server-side process | Client-side process |
| 1. Server sends plain HTML form to client | 1. Server sends HTML form containing processing script to client |
| 2. User fills out HTML form | 2. User fills out HTML form |
| 3. Client sends form data to server | 3. Processing can occur on the client during and after completing the form |
| 4. Form data processed on the server | 4. Client sends processed data to the server |
| 5. Server prepares plain HTML response page | 5. Server prepares plain HTML response page |
Advantages of server-side processing are:
Advantages of client-side processing are:
Actually, client-side scripting and server-side scripting can work together, first the scripting on the client, then on the server. We will use this method in this course.
ASP uses a common scheme in server-side scripting - the file that receives and processes the form data has regular HTML mixed in with proprietary scripting tags. The scripting tags can save data, send email, and also can write further HTML to the file. When the script is complete, the scripting tags are removed from the file, and the remaining HTML is the response page sent to the client. The original HTML is static, but the HTML written by the script can be dynamic, incorporating information from the form, and from elsewhere.
ASP can use several computer languages for processing. Here we will use the most popular, VBScript, for Visual Basic Script. VBScript can also be used client-side, but only Internet Explorer, not Netscape, can use VBScript, so it is not used very much for client-side scripting. ASP runs only on MS Windows web servers. You can tell when you are accessing ASP; the extension asp is at the end of the URL.
On the HTML page, ASP is contained within the tags <% (start) and %> (end). There can be many such pairs within a single file. For an ASP project of any size at all, the first line of the file should have the ASP directive <% Option Explicit %>. This requires that every parameter that you, the ASP developer, defines, must appear in a Dim statement, but more about that later. For now, Option Explicit catches many of your typos, a headache for all computer programmers.
Also, in computer languages, a section of text is called a string. Very often we will construct a string from various pieces, joined together by the concatenation operator, &. Some pieces can be literal, enclosed within quotes, while others can be dynamic, referenced by their name. If we build a text line to write today's date, that might be "Date: " & FormatDateTime(Now, vbShortDate) & "<br>" Here, Now is the current date and time according to the clock on the server, and vbShortDate tells the system to pick out the date part and write it in the brief format. On October 6 2003, this string would be "Date: 10/6/03<br>" where <br> is the HTML line break tag, so that this line could be put in the response page sent to the client.
Objects: Request and Response Objects
ASP is "object oriented," a concept in computer science that we will come back to. Briefly, objects are sort of like "things", and have "Properties" or data and "Methods," methods for doing things. Two of the most important ASP objects are
We can use this to tell the user what s/he put in the "FirstName" field in this way:
Response.Write "Hello, " & Request("FirstName") & "!<br>"
which for me would put the following line in the page that gets sent back to the client
Hello. David!
The <br> would start a new line in the response page, after !
Declaration of Parameters
Next we are going on to define parameters or variables in ASP. If you use <%Option Explicit%> as the first line of each ASP file, as suggested above, then any variable must occur in a Dim statement before its first use. This guards against typos, since if you mistype a variable's name, it will not have appeared first in the Dim statement. Here is a Dim statement for a variable named key.
Dim key
If we want to declare several variables, we can either do them in one Dim, in a comma-delimited list (list each, separated by commas) or give each its own Dim statement on a separate line, or spread them around several Dim statements, each with its own comma-delimited line. I like separate Dim statements, because they are easier to change, or at least I think they are.
Dim key
Dim lock
Dim chain
(What is this Dim? Some kind of Chinese food, maybe? No. Decades ago, when Microsoft first started working on the Basic languages, it stood for Dimension, which meant an array of things called a dimensioned variable, such as dog(5), a set of 5 things named dog(1), dog(2), ... dog(5). Somewhere along the way, it morphed into a more general term, that is still used for dimensioned variables, but also for just telling the system about any type of variable. Telling the system about a variable is called "declaring" the variable.)
Collections, Traversing Collections
A collection is a set of things or object that are grouped together. We can go through all of the objects in a collection (traverse the collection) of fields in the Form collection of the Request object, using a For ... Next loop as follows:
<%Option Explicit>
<html>
<head>
<title>List all form data</title>
</head>
<body>
Dim key
For each key in Request.Form
Response.Write key & ": " & Response.Form(key) & "<br>"
Next
</body>
</html>
If the HTML form had fields named FirstName and LastName, and I (David Bowen) had answered it, the response page would read:
FirstName: David
LastName: Bowen
B1: Submit
Yes, the Submit button is part of the form.
Some points about the ASP script above:
Built-ins
We used a built-in function to write the date above. Here is that one, along with one for the current time, according to the server's clock, and some other useful built-ins:
Hungarian Naming Convention
No, this is not a constitutional convention or anything like that. We will be using variables that represent a variety of things, and this is a convention for naming variables so that they remind us what they are. According to this convention, the first few letters are used to tell us what type of variable it is, and then comes the regular name. There are several variations, I use the one where the reminder is lower case, and then the first letter of the regular name is capitalized to separate it. Here are a couple of examples:
Connecting to, Storing in and Retrieving From a Database
In VBScript, there are several steps in connecting to a database. But first, we must understand a little about what a database is. Later we will go into this in more depth. But for now, a database is a file that contains pieces of data in rows and columns. Each column is a field, and holds a single type of information, such as someone's first name, last name, or birth date. A field in a database has a name, just like a field in an HTML form. The field names in the form and the database do not HAVE to match, although this is a very good idea as we will see soon enough. Each row is called a record, and involves all of the information about a single case, for example a single order. Records have numbers, but not names. So it works very well to store all of the data from a single submission of an HTML form into a record of a database. Each time the form is filled out, a new record is stored. Actually, the array of cells in rows and columns is called a table, and a single database can have several tables. In our case, though probably a single table in the database will do, but we have to have at least one table, and that table will have a name. The name is arbitrary, anything that makes sense to us. If a table records orders, for example, you can call it the Orders table.
A connection to a database is an object in VBScript. Since a database is a file, we can create a connection to the database with our Orders table by giving the path to the file; drive, folders, filename, extension (the extension is .mdb for Access). An alternate method, which we will use here, is called a Data Set Name or DSN. We will use this because it will hide the location of the database from any user who is smart enough to get access to your ASP script and read it. A DSN has a name, and putting the name in the right place in the code is all that the script needs to find, open, and use the database.
Databases can get to be huge files. Because of this, we work with a part of a database in RAM, called a recordset. If we want to add an order to the database, we create a recordset for the orders table, copy the form data into the recordset, and then update the database
Here is what the whole process looks like in code. Notice the comments; in VBScript, anything after a single quote is a comment, that you can use to remind yourself just what you are doing. Comments are great!
<%Option Explicit%>
<html>
<head>
<title>Record the order</title>
</head>
<body>
Dim cnConnect
Dim rsOrder
' Create the connection, then open it
Set cnConnect = Server.CreateObject("ADODB.Connection")
cnConnect.ConnectionString = "DSN=name of DSN"
cnConnect.Open
' Create the recordset, then open it
Set rsOrder = CreateObject("ADODB.RecordSet")
rsOrder.CursorLocation = 3 ' adUseClient or client-side cursor
rsOrder.Open "Orders", cnConnect, 2, 3 ' Open the Orders table for
use
' Add a new record to the recordset
rsOrder.AddNew
' Store First Name, Last Name and number of items ordered in the recordset
rsOrder.Fields("FirstName") = Request("FirstName")
rsOrder.Fields("LastName") = Request("LastName")
rsOrder.Fields("#Ordered") = Request("NumberOrdered")
' Notice that field names in database and form do not HAVE to match!
' Put the recordset into the database
rsOrder.Update ' Use the update method of the recordset
' Inform the customer
Response.Write Request("FirstName") & ", we have recorded your order.<br>"
</body>
</html>