Introduction to Web Design - 16. Active Server Pages
16.1 Objects | 16.2 An ASP Example
  1. Preface
  2. Markup Languages – A Definition and Some History
  3. Beginning HTML
  4. HTML Lists
  5. HTML Tables
  6. HTML - Color, Fonts and Special Characters
  7. HTML Links
  8. HTML Images
  9. HTML Frames
  10. Cascading Style Sheets
  11. MicroSoft PhotoDraw
  12. JavaScript
  13. HTML Forms and Form Handling
  14. VBScript
  15. MicroSoft FrontPage
  16. Active Server Pages
  17. Java Applets
  18. XML Meaning and More
  19. Macromedia Flash 5.0
  20. References
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, but JavaScript and other languages can be used instead. 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 Objects

ASP 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 Example

For 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>



The ASP file referred to in the HTML form above is listed in its entirety below. It is really a combination of VBScript and HTML. VBScript that is executed on the server is enclosed in <% … %> delimiters. Code within these delimiters is not executed on the client end, as is normally done with a VBScript or a JavaScript, but is executed on the server. The first line in the file with @LANGUAGE = VBScript, indicates to the server, that the script in this file is VBScript. If this line is omitted, the server will default to VBScript. Alternatively, JavaScript might be used as the scripting language. The next line contains the VBScript Option Explicit, command which is discussed in the section about VBScript, but, in short, it indicates that the programmer will be explicitly declaring all of the variables used in the script. Look at the code below. Further explanation of the code follows the code.

<% @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 = "martincc@cis.stvincent.edu"
 NewMail.From = "martincc@cis.stvincent.edu"
 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.

References

  1. Dietel, H. M., Dietel, P. J. & Neito, T. R. (2001) Internet & World Wide Web: How to Program. 2nd Edition. Prentice Hall, NJ.

Cynthia J. Martincic
cynthia.martincic@email.stvincent.edu
CIS Department
Saint Vincent College
Latrobe, PA 15650