ASP COMPONENTS

OVERVIEW

In this chapter, you will learn how to integrate other third-party components within your Web application, using the server object. These third-party components help you to incorporate more advanced features and functions into your Web application.

The three components that will be covered in detail in this chapter are:

  • the Browser Capabilities component, which is used to detect which features are supported by the current browser,
  • the AdRotator component, which is used to manage rotating banner ads, and
  • the CDONTS component, which is used to send e-mail from a Web page.

CHAPTER OBJECTIVES

Learn how the Component Object Model is used within a Web application

Use the server object to integrate your ASP page with ASP Components

Detect support for specific browser features using the Browser Capabilities component

Create a Web page that rotates banner ads using the AdRotator component

Send e-mail via an ASP page using the CDONTS object

Locate other third party ASP components on the Internet

ASP Components

A component is a piece of software code that defines objects properties, and methods, and encapsulates the entire code within itself. As a programmer you do not need to know the code, you just need to know how to plug-in to the code so you can use its features. 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.

COM objects are compiled programs written in programming languages such as Visual basic, C, C++, and Java. COM objects are packaged as executable programs (.exe) or dynamic-link library (.dll). In general, 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. Server-side COM objects are often referred to as server components, active server components, or ASP components. When the Microsoft Internet Information Server is installed, the ASP built-in objects (ASP object model: request, response, server, application, session, etc.) are installed.

If you would like to perform activities that are beyond the scope of the ASP object model, 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.


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 COM object 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”)

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) %>

After you have finished using the variable, it is highly recommended that you release the variable from memory by assigning the variable to the VBScript Nothing constant will free the memory used by the variable.

<% Set objName = Nothing %>

Browser Capabilities Component

The Browser Capabilities component refers to an external text file on the server known as the Browser Capabilities Detection File, which contains information about most of the browser software programs. The Browser Capabilities Detection File lists each browser with its version number and a list of features it supports.

<html<had<title>Browser Detection</title</head>

<body>

<h2>Browser Detection Example</h2>

<% dim bc

Set bc = server.createobject ("MSWC.BrowserType")

%>

Here is the value of the <br>

HTTP_USER_AGENT server variable:<br<br>

<% = request.servervariables("HTTP_USER_AGENT") %>

<br<br>

Here is the information from the <br>

Browser Capabilities component: <br>

Browser Name <% = bc.browser %<br>

Version: <% = bc.version %<br>

Platform: <% = bc.platform %<br>

<% if bc.vbscript = TRUE then %>

Welcome you support VBScript

<% else %>

You do not support VBScript, you are missing out.

<% end if %>

</body<html>

Syntax of Code / Explanation
Set bc = server.createobject ("MSWC.BrowserType") / This line of code instantiates a Browser Capabilities Component object.
<% = request.servervariables("HTTP_USER_AGENT") %> / This will display the HTTP_USER_AGENT server variable.
Browser Name <% = bc.browser %<br> / This line and the next two lines after this line display the browser name, the version, and the operating system of the client.
<% if bc.vbscript = TRUE then %> / This line of code checks the VBScript property on the variable. You can use this property to choose whether or not you can run your VBScript on a client.
QUICK FACT: / MSWC is the Microsoft Windows Component.

The AdRotator Component

You can use the AdRotator component to create and maintain banner advertisements on ASP pages. The AdRotator component provides a free, simple, easy-to-use tool for managing basic banner ads. The AdRotator component, called Adrot.dll, contains a method that can not only be used to display the ad, but also to manage the frequency with which the banner ad is displayed.

The information used to manage the ad is located in a single text file called the Rotator Schedule File, which means that one can change the ad across several Web pages simply by editing one file.

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 the user clicks the ad, the user is redirected to a new Web page by a Redirection File.

The Rotator Schedule File is a text file that is divided into two sections.

The first section defines the general parameters for the images of all banner ads. The syntax for this section is:

Redirect redirectpage.asp

Border n

Height n

Width n

*

The second section of the file contains four (4) parameters for each ad as it rotates into the Web page:

  1. Name of graphic file to be displayed
  2. The URL to redirect users when they click
  3. The text description of the ad for browsers without graphics capabilities, the ALT tag.
  4. The weighted relative frequency with which the ad is displayed

ImageURL/imageName.gif

The default text for the ALT tag

N

The * separates the two sections.

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 Query String. Below is a sample code that could be placed in the Redirection File.

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

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, but if this does not fall into your budget, 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.

The following sample Rotator Schedule File shows you how to complete step two.

Redirect adredirect.asp

Border 0

Height 67

Width 277

*

banner1.gif

Course Technology

60

banner2.gif

40

This Rotator Schedule File is set-up for two banners, banner1.gif and banner2.gif. The redirect file is named adredirect.asp, and the height and width are 67 and 277 respectively.

The third step is to create a new ASP page the will contain the server-side redirection.

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

<html<had<title>AdRotator</title</head>

<body>

<h2>Banner Ad Rotator</h2>

<% Dim objAd, varAd

set objAd = Server.CreateObject(MSWC.AdRotator")

varAd = objAd.GetAdvertisement("AdSchedule.txt")

Response.Write varAd %>

</body>

<html>

Syntax of Code / Explanation
<% Response.Redirect(Request.QueryString("url")) %> / This code is used to redirect the user to the URL retrieved from the hyperlink.
set objAd = Server.CreateObject(MSWC.AdRotator") / This line of code creates an instance of the AdRotator component by using the createobject method of the server object.
varAd = objAd.GetAdvertisement("AdSchedule.txt") / Then a variable named varAD will hold the information that will be used to create the image and anchor tags.
Response.Write varAd / This line of code writes out the information returned from the GetAdvertisement method.

The following code is created by the GetAdvertisment method, and can be viewed in the browser.

<a href=”adredirect.asp?url=

<img src=”banner1.gif” alt=”Course Technology” width=277 height=67 border=0</a>

Example:

This example contains four images to be rotated (banner1.gif, banner2.gif, banner3.gif, banner4.gif) on the myadpage.asp page. The Rotator Schedule File is called adschedule.txt. The Redirection File is named as adredirect.asp. All files are located in the same directory.

adschedule.txt

redirect adredirect.asp

border 0

height 67

width 277

*

banner1.gif

Course Technology

40

banner2.gif

Student Learning

30

banner3.gif

Web Warrior Series

20

banner4.gif

Academic Instructors

10

adredirect.asp

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

myadpage.asp

<html<head<title>Banner Ads</title</head>

<body>

<% Dim objAd, varAd

Set objAd = Server.CreateObject("MSWC.AdRotator")

varAd = objAd.GetAdvertisement("AdSchedule.txt")

response.write varAd

%>

</body>

</html>

View myadpage.asp

View adPage2.asp
The CDONTS Component

QUICK FACT: / CDONTS is only available with NT Server. It is not available with Personal Web Server or NT Workstation because SMTP will only work with NT Server.
You must have access to a directory on an NT 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 CDONTS component is an example of a third-party components. It is located in a server component called cdonts.dll, which is located in the c:\winnt\system32\ folder, and is made available when Microsoft SMTP server is installed.

The SMTP Server is included in the NT Option pack. The Simple Mail Transfer Protocol (SMTP) is a protocol, or a set of rules, used to transfer mail between servers. SMTP Server contains several default folders, including Pickup, Queue, Badmail, Drop, and SortTemp. These folders are located by default in the c:\Intetpub\MailRoot\ folder.

The Pickup folder is used to store outgoing mail messages. These messages are sent from SMTP Server to other servers. The default port for SMTP Server is 25. Note, mail messages can be placed in this directory by hand or by other programs such as cdonts.dll. The CDONTS component will place your message in the Pickup folder.

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 message.

The steps to create a new e-mail (programmatically) is similar to creating a physical e-mail, except there one needs to create an instance of the newmail object. First declare a variable to store the new mail object. Then use the createobject method of the server object to instantiate a newmail object.

Dim objMail

Set objMail = CreateObject(“CDONTS.Newmail”)

The newmail object has several properties that must be set before the messages can be sent. These are From, To, Subject, and Body. The codes below creates a Web page that sends a static message to a single receipient:

<html<had<title>Email sender</title</head>

<body>

<h2>Email Sender</h2>

<% Dim objMail

Set objMail = CreateObject(CDONTS.Newmail”)

objMail.From = “”

objMail.To = “”

objMail.Importance = 2

objMail.Subject = “CDONTS exercise”

objMail.Body = “This is a test message”

objMail.Send

Set objMail = nothing

%>

</body>

<html>

Syntax of Code / Explanation
Set objMail = CreateObject(CDONTS.Newmail”) / Instantiates the CDONTS object
objMail.To = “” / Sets the property to which the email shall be sent
objMail.Send / Sends the email
Set objMail = nothing / Frees the memory allocated

1