|Introduction to Web Design - 16. Active Server Pages|
|16.1 Objects | 16.2 An ASP Example|
Active Server Pages (ASP) is a server-side technology that can use server
and client information in order to deliver dynamically created HTML documents
to the client (user). VBScript is the most widely used language in which to implement
Active Server Pages are interpreted on the server by a scripting engine, which is most often
a MicroSoft proprietary ActiveX component with the file name asp.dll.
When an ASP file is requested by a user, the scripting engine interprets the code in the file.
Most often, the code generates HTML code, which is sent to the user and interpreted as an
HTML code by the user’s browser.
16.1 ObjectsASP has several objects that afford programmers access to user browser information, and user information submitted by GET or POST form methods (the Request object), that can send HTML code to a user (the Response object) and that can access server information (the Server object).
ASP also has File System Objects that represent files, folders, drives, and streams and a wide number of methods for each type of object.
16.2 An ASP ExampleFor this example, we will refer to the following HTML document which contains a form. The action of the form is an ASP document named submit-info.asp. Please familiarize yourself with the following form and then continue below.
<html> <!-- HTML form that requests an ASP document --> <head> <title>Personal Information</title> </head> <body> <form action = "submit-info.asp" method = "post"> First Name: <input type = "text" name = "fname" size = "20" /> <br /> Last Name: <input type = "text" name = "lname" size = "20" /> <br /> Street Address: <input type = "text" name = "straddress" size = "40" /> <br /> City: <input type = "text" name = "city" size = "30" /> <br /> State: <input type = "text" name = "state" size = "30" /> <br /> Zip: <input type = "text" name = "zip" size = "15" /> <br /> <input type = "submit" name = "submitButton" value = "Enter" /> </form> </body> </html>
<% @LANGUAGE = VBScript %> <% ' submit-info.asp Option Explicit %> <html> <head> <title>Information Submitted</title> </head> <body> <% Dim NewMail, Message Message = "First Name: " & Request( "fname") &_ "\n" & "Last Name: " & Request( "lname") &_ "\n" & "Street: " & Request( "straddress") &_ "\n" & "City: " & Request( "city") &_ "\n" & "State: " & Request( "state") &_ "\n" & "Zip: " & Request( "zip") Set NewMail = Server.CreateObject("CDONTS.NewMail") NewMail.To = "firstname.lastname@example.org" NewMail.From = "email@example.com" NewMail.Subject = "Information Submitted" NewMail.Body = Message NewMail.SendEmail() Set NewMail = Nothing //removes the object from memory %> <!-- retrieve and display textbox values --> <p>Hi <% =Request( "fname" ) %>, </p><br /> <strong> Welcome! </strong> <br /> The information you submitted: <br /> First Name: <% =Request( "fname" ) %> <br /> Last Name: <% =Request( "lname" ) %> <br /> Street Address: <% =Request( "straddress" ) %> <br /> City: <% =Request( "city" ) %> <br /> State: <% =Request( "state" ) %> <br /> Zip: <% =Request( "zip" ) %> <br /> </body> </html>
Within the body of the file, is a server-side script which concatenates all of the user information from the form into a variable called “Message”. Note that “Message” is a string that is concatenated with the ‘&’ symbol and that the code extends over several lines, so the line continuation character ‘_’ is used.
The “Request” object recovers the information posted by the user to the textboxes with the names used above as parameters to “Request”. “Request” returns the contents of the textboxes indicated by the names.
The script then creates a mail file object on the server, assigns values to various components of the mail file object and then calls the “SendEmail()” method to send the information via email to the webmaster.
The final line in the script removes the mail file object from memory.
The remainder of the file sends HTML code to the client to indicate the information that was submitted. Again, it uses the “Request” object to obtain the values of the submitted information.
Cynthia J. Martincic
Saint Vincent College
Latrobe, PA 15650