ASP Lecture Series (7) Using Components and Objects
Author:Eve Cole
Update Time:2009-05-30 19:59:15
ActiveX components are the key to building powerful Web applications. Components provide objects that are used in scripts to perform tasks. ASP also provides built-in objects that can be used in scripts. This topic discusses how to use objects provided by components as well as built-in objects provided directly by Active Server Pages.
About components
An ActiveX component is a file that contains code that performs a task or set of tasks. Components can perform common tasks so that you don't have to create the code to perform those tasks yourself. For example, a ticker component can display the latest stock quotes on a Web page. ASP comes with ready-to-use components, such as the Database Access component. Obtain optional components from third-party developers. Or you can write your own component.
You can leverage components as the basic building blocks for scripts and Web-based applications. Just know how to access the object provided by the component. Even a novice scripter can write scripts without understanding how components work. In summary, components enable you to write powerful scripts without learning to program.
If you are a Web application developer, you can encapsulate business logic by writing components. For example, you can write a component to calculate sales tax on a product. This component can then be called in scripts that process sales orders. Calculating tax rates independently during order processing allows you to update only the component when sales tax changes somewhere, without having to change the entire processing process. Components can be written in any language that supports the Component Object Model (COM), such as C, C++, Java, or Visual Basic. If you are familiar with COM programming, the ActiveX component is the Automation server. To run on a Web server, ActiveX components cannot have graphical user interface elements, such as Visual Basic's MsgBox function.
Components are reusable. Once a component is installed on a Web server, it can be called from an ASP script, an ISAPI application, another component on the server, or a program written in another COM-compatible language.
An instance component that generates a component object is executable code contained in a dynamic link library (.dll) or executable file (.exe). A component can provide one or more objects and their methods and properties. To use an object provided by a component, create an instance of the object and assign this new instance a variable name. Instances of objects can be created using ASP's Server.CreateObject method. Next, use the scripting language's variable assignment instructions to name the object instance. When creating an object instance, you must provide the registered name (PROGID) of the instance. For the basic components provided by ASP, the object's PROGID can be obtained from the reference page.
For example, ASP's Ad Rotator component loops through graphical ads. The Ad Rotator component provides an object called Ad Rotator, whose PROGID is "MSWC.AdRotator". To create an instance of an Ad Rotator object, use the following command:
VBScript:
<% Set MyAds = Server.CreateObject("MSWC.AdRotator") %>
JScript:
<% var MyAds = Server.CreateObject("MSWC.AdRotator") %>
If you are already very familiar with VBScript or JScript, you will notice that there are no scripting language functions for creating new object instances, such as CreateObject in VBScript or New in JScript. You must use ASP's Server.CreateObject method, otherwise ASP cannot track the use of objects in scripting languages.
Object instances can also be created using the HTML <OBJECT> tag. You must provide the server value for the RUNAT attribute and also provide the ID attribute group for the variable name that will be used in the scripting language. The object can be identified using the registration name (PROGID) or registration number (CLSID). The following example creates an instance of the Ad Rotator object using the registered name (PROGID):
<OBJECT RUNAT=Server ID=MyAd PROGID="MSWC.AdRotator"></OBJECT>
The following example creates an instance of the Ad Rotator object using the registration number (CLSID):
<OBJECT RUNAT=Server ID=MyAd
CLASSID="Clsid:1621F7C0-60AC-11CF-9427-444553540000"></OBJECT>
Creating an Object from a Java Class ActiveX components written in the Java language can be submitted as Java classes instead of DLLs. To use Server.CreateObject to create an instance of a Java class object, the class must be registered as a COM component using the Javareg program. You can then use Server.CreateObject with PROGID or CLSID.
If the object instance does not need to access ASP built-in objects and participate in transactions, it can call the Java class directly using the simpler mechanism provided by the Java monitor. You must use the Microsoft virtual machine for Java 2.0 (provided by the distribution of Internet Information Server and Personal Web Server) to use the Java monitor.
If you want to use a monitor to instantiate an object, you must use the VBScript or JScript GetObject command and provide the full name of the java class in the form java:classname. The following VBScript example generates an instance of a Java Date object.
<%
Dim date
Set date = GetObject("java:java.util.Date")
%>
<p> The date is <%= date.toString() %>
Objects generated by calling GetObject instead of Server.CreateObject cannot access ASP built-in objects and cannot participate in transactions.
Using ASP built-in objects
ASP provides built-in objects that perform tasks. For example, the Request object stores references from HTML tables.
Calling Object Methods Methods are activities that can be performed on or using an object. The general syntax for calling a method is:
Object.Method parameters
Parameters vary from method to method.
For example, you can use the Write method of the Response built-in object to send information to the browser by following the following instructions:
<% Response.Write "Hello World" %>
Note that some scripting languages do not support Object.Method syntax. If the language you are using does not support this syntax, you must make an entry in the registry in order to use that language as the primary scripting language.
Setting Object Properties Properties are characteristic values that describe an object. Properties define characteristics of an object (such as object type) or the state of an object (such as enabled or disabled). The general syntax is:
Object.Property
Property values can be read and set. For some objects, new properties can also be added.
For example, the Ad Rotator component has a Border property, which specifies whether the ad has a border and the thickness of the border. The following expression specifies no border:
<% MyAds.Border = 0 %>
You can use the ASP output directive to display the current values of certain properties. For example, the following command will return TRUE if the browser is still connected to the server:
<%= Response.IsClientConnected %>