Home

ASP ADVANCED

Active Server Pages includes a number of built-in objects and installable ActiveX components.  These objects and components can be used to extend the power of your Active Server Pages scripts.  But what exactly are objects and components?

An object is something that typically has methods, properties, or collections.  An object's methods determine the things you can do with the object.  An object's properties can be read or set to specify the state of the object.  An object's collections constitute different sets of key and value pairs related to the object.

To take an everyday example, the book Tom Sawyer is an example of an object.  The object has certain methods that determine the things you can do with it.  For example, you can read the book, use it as a doorstop, or even, if you're feeling particularly malicious, tear it into shreds.  The object has certain properties.  For example, it has a certain number of pages and a particular author. Finally, it has a collection of key and value pairs.  Each page number (the key), has a corresponding page of text (the value).

An ActiveX component is very similar to an Active Server Page built-in object.  However, when using Active Server Pages, there are two important differences between a component and an object.  First, a component may contain more than one object.  Second, an instance of a component must be explicitly created before it can be used.

Both the VBScript and JScript languages include a small number of objects.  For example, using either VBScript or JScript, you can access and manipulate the Dictionary object. The Dictionary object is not officially part of active Server Pages, but you can access it within your Active Server Pages scripts.  Here's an example using VBScript:

<%
Set MyDict=Server.CreateObject("Scripting.Dictionary")
MyDict.add "CA", "California"  
MyDict.add "MA", "Massachusetts"  
MyDict.add "MI", "Missouri"
%>
My dictionary has <%=MyDict.count %> entries.  
<BR>
The first entry in my dictionary is <%=MyDict.item("CA")%>.

When this script is executed, an instance of the Dictionary object is created.  Next, three key and value pairs are added to the dictionary.  Finally, two properties of the dictionary are displayed (see Figure below).

The first statement in this example illustrates the general method of creating a new instance of an ActiveX object by using Active Server Pages.  The method Server.CreateObject creates an instance of an object.  In this example, the variable named MyDict is assigned to an instance of the Dictionary object.

An example of the Dictionary object

An example of the Dictionary object

Once an instance of an object has been created, its methods can be called.  In this example, the Add method of the Dictionary object is called to add entries to the dictionary.  The first Add method call is used to add the key and value pair "CA" and "California".

After an instance of an object has been created, you can also access its properties.  In this example, the count property of the Dictionary object is read to determine the number of entries in the dictionary.  The Item property is also read to return the value of a particular key.

Finally, when you're done using an instance of an object, you can destroy it.  An object created by the preceding method will automatically be destroyed after the server finishes processing the Active Server Page.  Therefore, there's usually no reason to explicitly destroy an object.  However, you can destroy the MyDict object explicitly by assigning MyDict to a new value or by setting the variable to the value Nothing like this:

<% Set MyDict=Nothing %>

Although the Dictionary object is not officially part of Active Server Pages, the example above illustrates how you can use Active Server Pages objects.  You create an instance of an object by calling the Server.CreateObject( ) method. After you create a new object, you can call its methods and read and set its properties.

Active Server Pages Objects

Active Server Pages includes a number of built-in objects.  These objects allow you to extend the power of your scripts.  By using these objects, you can gain access to browser requests and control how the server responds to these requests.  The built-in objects also provide you with control over user sessions and Web server applications.

You have already been introduced to one example of a built-in object -- the Response object. You can use the Response object to send output to a browser.  However, the Response object also has a number of other important properties, collections, and methods.

The following chapters explain in detail how to use each of the built-in objects.  The following list provides a quick overview of each of the built-in objects:

  • The Application object.  The Application object is used to store and retrieve information that can be shared among all users of an application.  For example, you can use the Application object to pass information between users of your Web site.

  • The Request object. The Request object can be used to access all information sent in a request from a browser to your server. You can Use the Request object to retrieve the information that a user has entered into an HTML form.

  • The Response object.  The Response object is used to send information back to a browser.  You can use the Response object to send output from your scripts to a browser.

  • The server object.  The server object allows you to use various utility functions on the server.  For example, you can use the server object to control the length of time a script executes before it times out.  You can also use the server object to create instances of other objects.

  • The session object.  The session object can be used to store and retrieve information about particular user sessions.  You can use the session object to store information that persists over the course of a visit by a user to your Web site.

  • The ObjectContext object.  The ObjectContext object is used to control Active Server Pages transactions.  The transactions are managed by the Microsoft Transaction Server (MTS).

The built-in objects differ from normal objects.  You don't need to create an instance of a built-in object before you can use it in a script.  The methods, collections, and properties of a built-in object are automatically accessible throughout a Web site application.

Active Server Pages Components

Like the built-in objects discussed in the preceding section, Active Server Pages components can be used to extend the power of your scripts.  Components differ from the built-in objects because they're typically used for more specialized tasks.  The following list provides a brief overview of some of the components bundled with Active Server Pages:

  • The Ad Rotator component.  The Ad Rotator component is used to display banner advertisements on the Web pages of a Web site.  You can use this component to specify how frequently different banner advertisements should be displayed.

  • The Browser Capabilities component.  The Browser Capabilities component can be used to display different HTML content, according to the capabilities of different browsers.  For example, you can use this component to display Web pages with frames only to frames-compliant browsers.

  • The Content Linking component.  Using the Content Linking component, you can link together a number of HTML pages so that they can be navigated easily.  For example, you can use this component to display the pages of an online book.

  • The Counters component.  The Counters component can be used to keep track of the number of visitors to your Web site.  You can use the Counters component to add a hit counter to a Web page.

  • The Content Rotator component.  The Content Rotator component enables you to rotate through HTML content on a page.  For example, you can use the component to randomly display different announcements on the home page of your Web site.

  • The Page Counter component.  Exactly like the Counters component, the Page Counter component can be used to track the number of visitors to a Web page.  You can use this component to add a hit counter to a particular Web page.

  • The Permission Checker component.  The Permission Checker component can be used to display links to Web pages only if a user has permission to see them.  You can use this component to create Web pages that can be viewed only by the administrators of a Web site.

  • The ActiveX Data Objects.  The ActiveX Data Objects (ADO) enable you to retrieve and store data in a database such as Microsoft SQL Server.  These objects are extremely important.  For this reasoon, they are covered in a separate section, where each of the objects in this group is thoroughly discussed.