Using Components and Objects

ActiveX components are the key to building powerful Web applications. Components provide objects that you use in your scripts to perform tasks. ASP also provides built-in objects that you can use in your scripts. This topic explains how to use the objects provided by any component as well as the built-in objects provided directly by Active Server Pages. For information on using an object's collections, see Using Collections.

About Components

An ActiveX component is a file containing code that performs a task or set of tasks. Components perform common tasks so that you do not have to create your own code to perform these tasks. For example, a stock ticker component might display the latest stock quotes on a Web page. ASP comes with base components that you can use immediately, such as the Database Access Component. You can obtain additional components from third-party developers. Or, you can write your own components.

You use components as the basic building blocks of your script or Web-based application. If you are new to scripting, you can write scripts that use components without knowing anything about how the component works. You simply need to know how to access the objects provided by the components. Components enable you to write powerful scripts without learning any programming.

If you are a Web application developer, you can write components to encapsulate business logic. For example, you could write a component to calculate the sales tax on a product sale. You could then call the component from a script that processes a sales order. Isolating the tax calculation from the order process enables you to update the component when the sales tax for an area changes without changing your order process. You can write components in any programming language that supports the Component Object Model (COM), such as C, C++, Java, or Visual Basic. If you are familiar with COM programming, ActiveX components are Automation servers. To run on the Web server, your ActiveX components cannot have any graphical user interface elements, such as the Visual Basic MsgBox function.

Components are reusable. Once you have installed a component on your Web server, you can call it from an ASP script, an ISAPI application, another component on the server, or a program written in another COM-compatible language.

Creating an Instance of a Component’s Object

A component is executable code contained in a dynamic-link library (.dll) or in an executable (.exe) file. A component provides one or more objects, along with the object’s methods and properties. To use an object provided by a component, you create an instance of the object and assign the new instance to a variable name. Use the ASP Server.CreateObject method to create the object instance. Use your scripting language’s variable assignment statement to give your object instance a name. When you create the object instance, you must provide its registered name (PROGID). For the base components provided with ASP, you can get the PROGID for the objects from the reference pages.

For example, the ASP Ad Rotator component randomly rotates through a series of graphical advertisements. The Ad Rotator component provides one object, called the Ad Rotator object, whose PROGID is "MSWC.AdRotator". To create an instance of the Ad Rotator object, use one of the following statements:

VBScript:

<% Set MyAds = Server.CreateObject("MSWC.AdRotator") %>

JScript:

<% var MyAds = Server.CreateObject("MSWC.AdRotator") %>

If you are already familiar with VBScript or JScript, notice that you do not use the scripting language’s function for creating a new object instance (CreateObject in VBScript or New in JScript). You must use the ASP Server.CreateObject method; otherwise, ASP cannot track your use of the object in your scripts.

You can also use the HTML <OBJECT> tag to create an object instance. You must supply the RUNAT attribute with a value of Server, and you must provide the ID attribute set to the variable name you will use in your scripts. You can identify the object by using either its registered name (PROGID) or its registered number (CLSID).The following example uses the registered name (PROGID) to create an instance of the Ad Rotator object:

<OBJECT RUNAT=Server ID=MyAd PROGID="MSWC.AdRotator"></OBJECT>

The following example uses the registered number (CLSID) to create an instance of the Ad Rotator object:

<OBJECT RUNAT=Server  ID=MyAd 
CLASSID="Clsid:1621F7C0-60AC-11CF-9427-444553540000"></OBJECT> 

Creating an Object from a Java Class

An ActiveX component written in Java can be delivered as a Java class instead of a DLL. To use Server.CreateObject to create an instance of an object exposed by the Java class, you must use the Javareg program to register the class as a COM component. You can then use Server.CreateObject with the PROGID or CLSID.

If the object instance does not need access to the ASP built-in objects and does not need to participate in a transaction, you can use the simpler mechanism provided by Java monikers to call the Java class directly. You must be using version 2.0 of the Microsoft virtual machine for Java (provided with this release of Internet Information Server and Personal Web Server) to use Java monikers.

To instantiate an object by using monikers, use the VBScript or JScript GetObject statement and provide the full name of the Java class in the form java:classname. The following VBScript example creates an instance of the Java Date object.

<% 
Dim date
Set date = GetObject("java:java.util.Date")
%>

<p> The date is <%= date.toString() %>

Objects created by calling GetObject instead of Server.CreateObject cannot access the ASP built-in objects and cannot participate in a transaction.

Using ASP Built-in Objects

ASP provides built-in objects that perform tasks. For example, the Request object stores the data passed to the server from an HTML form. Unlike using the objects provided by an ActiveX component, you do not need to create an instance of an ASP built-in object to use it in your scripts. You access the methods and properties of a built-in object in the same way in which you access the methods and properties of a component’s objects, as described in this topic.

Calling an Object Method

A method is an action you can perform on an object or with an object. The general syntax for calling a method is:

Object.Method parameters

The parameters vary depending on the method.

For example, you can use the Write method of the Response built-in object to send information to the browser as shown in the following statement:

<% Response.Write "Hello World" %>

Note   Some scripting languages do not support the Object.Methodsyntax. If your language does not, you must add an entry to the registry in order to use that language as your primary scripting language. See Working with Scripting Languages for more information.

Setting an Object Property

A property is an attribute that describes the object. Properties define object characteristics, such as the type of the object, or describe the state of an object, such as enabled or disabled. The general syntax is:

Object.Property

You can read the value of a property and set the value of a property. For some objects, you can also add new properties.

For example, the Ad Rotator component has a property, Border, that specifies whether the ad has a border around it and determines the border thickness. The following expression specifies no border:

<% MyAds.Border = 0 %>

For some properties, you can display the current value by using the ASP output directive. For example, the following command returns TRUE if the browser is still connected to the server:

<%= Response.IsClientConnected %>