CIS 209 Web Page Scripting Languages

Chapter 10: ASP Components

In previous chapters, we've learned to use built-in objects to write to the Web browser and process a form. In this chapter, we will learn how to integrate other third-party components using the server object. We'll take a close look at three components in this chapter; Browser Capabilities component, AdRotator component, and the CDONTS component. Finally, we'll locate Internet resources that will help to build and implement third-party components.

ASP Components

A component is a piece of software code that defines objects, properties and methods, and encapsulates the entire code within itself. When objects are encapsulated, the programmer does not need to know or understand the code used to create the object. Reusable code is one of the benefits of object-oriented programming.

Components that conform to the Component Object Model (COM) are known as COM objects. The Component Object Model is a standard that allows independent components to communicate with each other. The Component Object Model defines how COM objects interact with other COM objects.

COM objects are compiled programs often written in programming languages such as Visual Basic, C, C++, and Java. COM objects are packaged as executable programs (.exe) or dynamic-link libraries (.dll). Executables provide better security, while dynamic-link libraries provide better performance.

COM objects can be installed on a client's computer or on the server. Client-side COM objects are often referred to as ActiveX controls. Although NCompass Labs wrote a plug-in to allow Netscape to support some ActiveX controls, Internet Explorer is the only browser that fully supports ActiveX controls. ActiveX controls are supported by using the <OBJECT> tag to add an ActiveX control to a Web page.

Server-side COM objects are often referred to as server components, active server components or ASP components. They are simply COM objects installed on the server.

If you would like to perform activities that are beyond the scope of the ASP built-in objects, you can use ASP components that other developers have written. Microsoft provides several additional ASP components, including Browser Capabilities component, the AdRotator component, the Database Access component, and the CDONTS component.

The Browser capabilities component identifies the browser software and determines which features it supports. The AdRotator component allows you to create rotating banner ads on a Web page. The Data Access component allows you to create Web pages that interact with your database.

Third-party software such as the Microsoft SMTP Server contains ASP components that can be used to interface with their software. The SMTP Server uses the CDONTS Component to interface with the SMTP Server. You can use the CDONTS component to send email from an ASP page. You can also create your own ASP components and implement them in your Web application.

Because COM objects communicate with each other by other means of the COM standards, the programmer only needs to interface to the COM object. For example, the programmer only needs to integrate the ASP pages with the Data Access component (DAC). The database server does not have to reside on the same physical server as the Web server.

Integrating ASP Components into ASP Pages

Because ASP components are COM components, they can contain predefined objects, properties, and methods. After the ASP components are installed, your Web pages can access their objects, properties, and methods. Like built-in objects, the ASP component objects are not accessed directly.

The ASP built-in server object contains a method called createobject that allows you to create an instance of an object from any ASP page. The component name is used to identify the type of object to instantiate. The actual component name will vary with the type of component installed. For example, the component name for the Browser Capabilities component is MSWC.BrowserType.

The new instance of the object is assigned to a variable. You can then refer to the variable to call methods and set the properties of the object. To create an instance of an ASP component, use the following syntax:

dim objVariableName
set objVariableName = server.createobject("ComponentName")

The COM object (or COM component) can be instantiated using the server object’s createobject method, or the <object> tag. The syntax below is an example of how to create an instance of the Browser Capabilities component.

dim objBrowCap
set objBrowCap = server.createobject(“MSWC.BrowserType”)

The object can be created on the server using the <OBJECT> tag in the Global Application File. The <OBJECT> tag is an HTML tag.

<object runat="server" Scope="Application" PROGID="ComponentName" id="objName"></object>

We can use the following code to call an object and set its properties within the scope of the application.

<% Application("objName").Property=value %>

Although page-level objects are easily scalable, creating objects and storing them in session or application variables is generally not recommended.

Assigning Properties and Calling Methods

Once you have created the instance of the object using the component, you can access its properties and methods by referring to the name of the variable and the name of the property or method. The syntax below illustrates how to retrieve the property value of an object, store it in a variable, and write it to the Web page.

<% varProperty = objName.PropertyName %>
<% = varProperty %>

The following syntax illustrates how to assign a value to a property of an object.

<% objName.PropertyName = expression %>

The syntax below illustrates how to call a method of an object.

<% objName.Methodname Arg1, Arg2….,ArgN %>

And if there is going to be a return value use the following syntax.

<% returnValue = objName.Methodname (Arg1, Arg2….,ArgN) %>

When you assign an object to a variable, you are assigning a memory location to the object. After you have finished using the variable, it is highly recommended that you release the variable from memory so the memory space can be reused. Assigning the variable to the VBScript Nothing constant will free the memory used by the variable.

<% Set objName = Nothing %>

Be sure to close objects and set them to Nothing after they are no longer needed. Database connection objects that are left open continue to remain open until they are closed. Not only will you be wasting memory, but you will also have a connection that is not being used, which can affect performance.

The Browser Capabilities Component

The HTTP_USER_AGENT server variable is passed in the HTTP header and contains information about the client's Web browser. However, to use this information, we have to parse the returned string.

The Browser Capabilities component parses the HTTP_USER_AGENT to provide information about the client's browser such as its type, version number and what features the browser supports. The Browser Capabilities component is a program named browsercap.dll, which is installed on the Web server.

The Browser Capabilities component refers to an external text file on the server known as the Browser Capabilities Detection File which contains information about various browsers on the market. HERE (browscap.ini) is the Browser Capabilities Detection File that is installed on IIS5 by default. As you can see, if a feature is supported, it is set to TRUE and it the feature is set to FALSE if it is not supported. While browsecap.ini contains many of the features supported by browsers, it does not contain all of them.

You can update the Browser Capabilities Detection File to the latest version from third party developers such as cyScape, who also sells the BrowserHawk component that can detect more features for a wider selection of browsers. After downloading and extracting the ZIP file, just copy the new browsercap.ini file in the c:\winnt\system32\inetserv folder, overwriting the older file.

Learn more about the Browser Capabilities component from http://msdn.microsoft.com/library/psdk/iisref/comp3xx0.htm.

Here is an EXAMPLE of how we can detect the browser features using the Browser Capabilities component.

The AdRotator Component

You can use the AdRotator component to create and maintain banner advertisements on ASP pages. There are companies that sell third party server objects that provide more advanced management features, such as target-based marketing. However, the AdRotator component is a free, simple and easy-to-use tool for basic banner ads.

The AdRotator component, called Adrot.dll, contains a method that you can use to display ads and manage the frequency with which the banner ads are displayed.

When a user visits an ASP page that has a banner ad, an instance of the AdRotator object is created. The ASP page uses the Rotator Schedule File to determine which ad to display. When users click the ad, they are redirected to a new Web page by the Redirection File.

Learn more about the AdRotator component from http://msdn.microsoft.com/library/psdk/iisref/asps4v5e.htm or http://msdn.microsoft.com/library/psdk/iisref/comp59f8.htm.

Creating the Rotator Schedule File

The Rotator Schedule File is a text file that contains information about the ads to be displayed on the ASP page. It is divided into two sections.

The * separates the two sections. The file is often named Adrot.txt or AdSchedule.txt.

Creating the Redirection File

The Redirection File name must match the page identified in the first line of the Rotator Schedule File. When users click the image in the ASP page, they are redirected to the Redirection File. The Redirection File retrieves the URL for the link from the QueryString. Below is a sample code that could be placed in the Redirection File.

<% Response.Redirect(Request.QueryString(“url”)) %>

We could modify the Redirection File to determine whether there is a value for the URL before redirecting as demonstrated below:

<% If Request.QueryString("url") <> "" Then
     Response.Redirect(Request.QueryString("url"))
End If %>

Creating the ASP Page to Display the Banner Ads

There are three steps to create a Web page that displays a banner ad using the AdRotator Component.

  1. Create the banner graphics
  2. Create the Rotator Schedule File (both sections)
  3. Create a new ASP page that will contain the server-side redirection

A graphic artist usually takes care of step one or you may want to use a Web site that has a banner generator to create the banner. The following Web site provides a banner generator.
http://www.coder.com/creations/banner/banner-form.pl.cgi.

In the example from out text, a variable named objAd stores the new AdRotator object. An instance of the AdRotator component is created using the createobject method of the server object. The name of the AdRotator component is MSWC.AdRotator. MSWC is the Microsoft Windows Component.

Check out these two examples from http://www.w3schools.com/asp/asp_adrotator.asp.

Banner Ad Graphics

You can create your own banner ads using a graphics program. A couple of commercial graphics programs on the market today include Paint Shop Pro from http://www.jasc.com and Photoshop from http://www.adobe.com. There are many sites on the Web today that contain free graphics, banners, icons, buttons and animated graphics. See http://www.tucows.com and http://www.download.com for downloadable freeware and shareware graphics editing programs.

Creating a Banner Ad

Pages 374 to 376 provides steps to create a banner ad using a graphic created by visiting Coder.com.

The CDONTS Component

Careful! Please do not abuse my account!

I am making the CDONTS component available to our class so that we can experiment with it and learn about it. Due to the fact that this component actually sends out email from my personal account using my ISP, I could have my account terminated if anyone in our class started sending out spam which would eventually get traced back to me via my IP address. Please use this component with GREAT CARE!!!

Please do not use the CDONTS component to email anyone except yourself!

When you use the CDONTS component in your A## account on our class server, you will actually be sending email from my personal account. At first glance, it may not appear that it came from me, but if you look closely at the email header information, you will find that it came from my IP.

CDONTS is a collection of objects, properties and methods that connect the SMTP Service with the Web application.

CDONTS is only available with NT Server or Windows 2000 Server. It is not available with Personal Web Server or NT Workstation because SMTP will only work with Servers. You must have access to a directory on an NT or Windows 2000 Server with an SMTP Server running and with CDONTS installed in order to complete the activities in this section. CDONTS is also available with Windows 2000 Professional using IIS. The Simple Mail Transfer Protocol (SMTP) is a protocol, or set of rules, used to transfer mail between servers.

Some hosting service providers do not use the SMTP mail server. Some HSPs use SMTP, but require you to use a different mail component. For example, ServerObject Inc. at  http://www.serverobjects.com, provides two mail components named AspMail and AspQMail that are commonly used to enable ASP to send email.

The CDONTS component us a scaled-down version of a collection of objects known as Collaborative Data Objects (CDO) which is used to integrate your applications with active messaging systems such as Microsoft Outlook and Microsoft Exchange Services.

Check out Microsoft's MSDN Library for more details about the CDONTS object and take a look at the following article at http://www.microsoft.com/Mind/0299/email/EMAIL.ASP.

Creating Web Pages That Send E-mail

The CDONTS component contains an object named newmail, and newmail contains a method called send, which is used to create the e-mail message and send it to the Pickup folder. From there SMTP Server will retrieve and send the email message.

We use the createobject method of the server object to instantiate a new newmail object. The name of the CDONTS component is CDONTS.NewMail.

Dim objMail
Set objMail=CreateObject("CDONTS.Newmail")

The newmail object has several properties:

objMail.From="sender@senderaddress.com"
objMail.To="recipient@recipient.com"
objMail.Subject="Subject"
objMail.Body="Body of the email message"

The importance property can also be set. The importance property will not send mail faster. Three importance property leves that you can set with the new mail object are 1 (cdohigh), 1 (cdoNormal), and 0 (cdoLow).

We use the send method of the newmail object to send the mail. Actually, the send method creates the message in the required format and writes it to the Pickup folder where the SMTP Server will deliver the message.

objMail.Send
Set objMail=Nothing

DON'T FORGET TO SET THE OBJECT TO NOTHING TO RELEASE THE OBJECT FROM MEMORY!

JUST A REMINDER...
Set objMail=Nothing  
Set objMail=Nothing
Set objMail=Nothing
Set objMail=Nothing
Set objMail=Nothing
Set objMail=Nothing
Set objMail=Nothing

Or, we can pass the properties as parameters of the send method instead of defining each property individually. The order of the parameters must be: SenderEmailAddress, RecipientEmailAddress, Subject, and BodyOfMessage. The following example illustrates how an email can be sent in just THREE lines of code:

Set objMail=CreateObject("CDONTS.Newmail")
objMail.Send"sender@sender.com", "recipient@recipient.com", "Subject", "Body of Message"
Set objMail=Nothing

Note: The book has an error on page 378 at the bottom of the page, just above the heading "Additional Properties of the Newmail Object" in the line which reads:

objMail.Send("sender@sender.com", "recipient@recipient.com", "Subject", "Body of Message")

The parentheses are not allowed. You will get "Cannot use parentheses when calling a sub" error. To fix it, just delete the parentheses.

Check out the 282ln10SendMail.asp example.

Additional Properties of the Newmail Object

You can specify multiple recipients in the TO property by separating addresses with semicolons (;). We can add a "carbon copy" (CC) to be sent to another recipient with the CC property. To keep the recipient from knowing that other people have been sent the same message, we can add a "blind carbon copy" (BCC) with the BCC property.

We can use the importance property to indicate to the email application the importance of the message. Some email programs, such as Outlook, include a field which indicates the importance of an email. The importance property will not send email faster :o) 

The three importance property levels that we can set with the newmail object are 2 (cdoHigh), 1 (cdoNormal) and 0 (cdoLow). You can use the numbers 2, 1 or 0    OR   you can use the constants if you add the server-side include file that contains the constants. See http://support.microsoft.com/support/kb/articles/Q218/6/07.asp.

Here is the syntax for setting these additional properties:

objMail.To="recipient1@recipient1.com;recipient2@recipient2.com"
objMail.CC="carbonCopyRecipient@CarbonCopyRecipient.com"
objMail.BCC="secretRecipient@secretRecipient.com"
objMail.Importance=ImportanceLevel

Sending E-mail From a Web Page

Often, companies want to provide information to prospects by allowing their prospects to click a link that will automatically, and immediately, send them the information via email. These auto responders are a good way to provide immediate feedback to customers automatically. We could easily set up a link to an ASP page which automatically sends out a static message containing product information, company information, etc.

Sending E-mail with HTML: By default, the newmail object formats the message body as "basic text", which is set using the constant CdoBodyFormatText, or the number 1. We can change the message body format to HTML by using the constant CdoBodyFormatHTML or the number zero (0). The syntax to send email using the body format HTML is:

myText="<b>This is bold</b> because the message supports HTML"
objMail.BodyFormat=0
objMail.Body=myText

Sending E-mail With Attachments: The newmail object has a method called AttachFile, which allows you to attach files to your email messages. The AttachFile method has three parameters:

  1. the location property - identifies the absolute location of the attached file
  2. the Caption property - is a default caption
  3. the Encoding property - is the type of encoding

To use the AttachFile method to send a graphic as an attachment, we can use the following syntax:

objMail.AttachFile("c:\images\disk.gif","Disk Image")    OR
objMail.AttachFile("c:\images\disk.gif","Disk Image",cdoEncodingBase64)

Locating Other Third-Party Software Components on the Internet

CyScape (http://www.cyscape.com) created the BrowserHawk component to provide additional features beyond those provided by the Browser Capabilities component. ServerObjects (http://www.serverobjects.com) provides several ASP components such as ASPQMail and ASPMail which are similar to CDONTS. While Microsoft provides a basic FileUpload component, Software Artisans (http://www.aspstudio.com) provides the SA-FileUp component to upload files from ASP pages to the Web server. Information about other components that Microsoft provides can be found at http://msdn.microsoft.com/library/psdk/iisref/comp275c.htm. IISDev (http://www.alphasierrapapa.com/IisDev/Components) provides several free components that work with IIS. Hunt Interactive (http://www.huntinteractive.com) provides ASP components which make it easier to ASP scripts within Visual InterDev. You can build your own components using VB & C++.

Installing ASP Components

Built in components are installed automatically when IIS is installed. Server components must be listed in the registry of the computer that will run the component. The registry contains information that is required for the program to communicate with other COM objects installed on the computer. You must have administrative rights and access to the server to install and register a COM component.

We can use the Registry Installation Wizard to install components, or use the command line program called Windows Regsvr32. You should find the file named regsvr32.exe in the SYSTEM32 directory under the WINNT folder on Windows 2000 Server.

ASP is evolving into a newer technology called ASP+. ASP+ will be backward compatible and support ASP 3.0. One of the benefits of ASP+ is that the COM objects do not have to be registered.