Web based School

Chapter 17

Using Active Server Components to Create Multitier Applications

As technology continues to change rapidly, new developments keep improving the lives of both producers (developers) and consumers (end-users). You have discovered many of the new developments for Web-based application development through the lessons in this guide. ToChapter's lesson continues exploring new developments by introducing you to the world of Active Server Components, previously known as OLE Automation Servers. Visual Basic developers are probably familiar with the concept of OLE Automation Servers, which were introduced in Visual Basic 4.0. Just as ActiveX replaced OLE controls, Active Server Components have become the new and improved version of OLE Automation Servers. These new and powerful components can be integrated into your Active Server Pages, enabling you to create robust applications that handle your users' processing needs. At the beginning of the first week, the lesson's main premise was that the Web had moved from a publishing metaphor to one of true interaction. Active Server Components help streamline this activity by giving your application robust functionality.

ToChapter's lesson gives you insight into many topics. First, you gain an understanding of the Component Object Model and the Distributed Component Object Model from Microsoft. Next, you learn how to define and explain Active Server Components; you also learn about their main benefits and strengths for use in your application. The lesson then teaches you how to include and integrate Active Server Components into your Active Server Pages. This part of the lesson explains how to capitalize on these components' power to meet the needs of your application.

An Architecture Review: COM and DCOM

Microsoft developed the Component Object Model (COM) to supply a common method for communication between objects. COM's roots can be traced back to OLE technology, also developed by Microsoft. COM enables objects to communicate with each other through effective dialogue, thereby forming a synergistic relationship. With true dialogue, each object can understand the other object's interfaces, methods, and services so that they can work together for the good of the end-user. Two terms concerning COM technology are used throughout this part of the lesson: producer and consumer. The producer represents the object that provides the basic interfaces, methods, and services to another object; the consumer is the object or application that uses, or consumes, the services of the producer object. Some objects alternate being the producer and consumer, based on their capabilities and the relationship established between the two objects. An example of this kind of relationship is a project team consisting of many different team members who have certain unique skills. Each team member knows the strengths and roles of the other people on the team. Also, standard communication methods--e-mail, meetings, and telephone conversations--have been established to facilitate sharing knowledge. The team members communicate and share their abilities and services to create an application.

Other communication is strictly one-way in nature, such as communication in a relationship between a coffee shop and its customers. The coffee shop has communicated its message, advertising itself as the place to buy superb coffee drinks. Customers, including myself, realize the service it provides and consume drinks such as cafe mochas in large quantities. This kind of communication is similar to a COM object that advertises, or publishes, its unique services, while the client application consumes, or uses, these services.

New Term: A producer is an object that implements and provides certain methods and services through standard interfaces. COM is used to make these services available to other objects.

New Term: A consumer is an object that uses the services of a producer object. This type of object can also be referred to as a client or container object.

Objects use COM on the same machine. However, Microsoft has extended COM's reaches by creating the Distributed Component Object Model, or DCOM, which enables you to communicate with a COM object on another machine. The following sections explain COM and DCOM in more depth and cover the benefits of each technology.

NOTE: This discussion on COM and DCOM is meant to briefly introduce these exciting and robust technologies. It only begins to scratch the surface on these topics. This section gives you a foundation for the rest of toChapter's lesson, which focuses on Active Server Components. For more information on ActiveX, COM, and DCOM, I highly recommend Understanding ActiveX and OLE by David Chappell.

Understanding COM

In the previous section, you learned that COM objects operate on the same machine and can be used as in-process servers or local servers. In-process server objects provide their code in a dynamic link library (DLL) that executes in the same process as the consumer object. Figure 17.1 demonstrates the communication between a consumer and a producer that has been implemented as an in-process server.

Figure 17.1.

An in-process server.

Because an in-process server is implemented as a DLL, the objects in Figure 17.1 operate within the same address space in memory, so they can share information more easily.

You can also use a COM object as a local server that resides as a separate process running on the same machine as the consumer or client application. Figure 17.2 illustrates the communication process for a local server object.

Figure 17.2.

A local server.

As you can see, the COM objects communicate through a special type of remote procedure call: LRPC, or local/lightweight remote procedure call. LRPCs offer a way for COM objects executing in separate address spaces in memory to communicate with each other. The LRPC communication mechanism indicates that the communication is local to the machine. In the next section, you learn another name for communication across multiple machines.

COM offers several benefits to a developer. First, COM enables you to create reusable components for your application. You can use COM's power to create separate software components that your application can access when necessary, thereby avoiding redundancy and duplication in individual code modules. In this way, the processing logic in the COM component is isolated from the application code, so you can change the component without affecting the application. This process has two main implications. First, you can separate the true business and database logic from your presentation logic, which takes advantage of a three-tier application model. Second, you don't have to recompile and redistribute your application whenever the logic in the component changes. In other words, the component centralizes the process for software updates, giving you a simpler approach to version control.

Another benefit of COM is that it offers a consistent method for objects to converse with each other. Using a standard form of communication means you know an object's message is received, understood, and incorporated into the life of the other object. Also, since COM objects implement a binary runtime interface, you can develop a COM object with one language and consume its services with an object or application created with another language. This benefit shows you that COM is a language-independent model for application and component development.

Understanding DCOM

Microsoft's DCOM is founded on the same principles as COM but extends these principles across multiple machines; its main design goal is to make use of the same powerful characteristics of COM across machines in your network. DCOM enables objects on different machines to communicate, thereby integrating COM's communications strengths and the network.

DCOM also opens up new possibilities for robust application processing. The first uses of OLE and COM were embedding a spreadsheet into a word processing document. Once users got over the novelty of this feature, they wanted the technology to deliver more functionality. DCOM capitalizes on the strengths of COM so you can offer the robust functionality your users have been clamoring for. You can integrate DCOM objects and components on a server machine to give your applications powerful server programs. Later in toChapter's lesson, you learn how to integrate DCOM components into an application.

DCOM components are considered remote servers because they're implemented remotely on a different machine from the client consumer machine. You can implement a DCOM object as a DLL or a separate executable process.

DCOM uses Object Remote Procedure Calls, or ORPCs, as the communication vehicle for different machine objects, as shown in Figure 17.3. By using ORPCs, you can make com-munication between objects easier and cause the producer object to seem as though it's local to the consumer. This process is like virtual shopping, where you electronically move through a store, picking out the items you want to buy; this simulated shopping makes it seem as though you're actually in the store selecting items. DCOM provides tight security to make sure only authorized users can activate a specified DCOM component.

Microsoft has recently included DCOM with its Windows NT Server operating system, which should rapidly increase the number of DCOM applications being developed. You can make use of DCOM's strengths to integrate your Web-based applications with existing client-server and legacy systems. The following sections cover the capabilities of Active Server Components and Active Server Pages.

Figure 17.3.

The art of DCOM.

What Is an Active Server Component?

Now that you understand the basic concepts of COM and DCOM, you can go on to learn about Active Server Components. ToChapter's lesson also describes the basic types of objects and components included as part of the Active Server Scripting model.

Active Server Components are objects or components implemented as part of your Web-based application with an Active Server Page. You can develop Active Server Components to exist as in-process DLLs or out-of-process executable programs, in addition to local or remote servers. A common example of a local server component is a DLL that executes within an Active Server Page (ASP). You might also want to create an Active Server Component that uses DCOM to integrate your Web-based application with a legacy application, such as an accounting system. In this case, the Active Server Component resides on a separate machine from the web server.

Visual InterDev includes several intrinsic and extrinsic objects included as part of the Active Server Pages model, previously code-named Denali. The intrinsic objects, covered previously on Chapter 11, "Extending Your Application Through Active Server Script," include the Request, Response, Server, Application, and Session objects. The Active Server Pages model also includes several extrinsic objects that can be used in your application. These extrinsic objects, or components, give you prepackaged functionality, such as checking a client browser's capabilities, providing database access, and rotating the display of ads. The following sections outline the uses and features of these extrinsic objects.

The Ad Rotator Component

The AdRotator object enables you to rotate the display of advertising images on a web page. The object name of this component is MSWC.AdRotator. This feature has probably been implemented on many of the sites you have visited, such as the Microsoft home site at this URL:


You can specify the images in a text file and use the GetAdvertisement method to display a specific advertising image. You can also designate a URL link for the image so the user can travel to the web site indicated by the ad. The GetAdvertisement method uses the information in the text file, called an ad schedule file, to generate the HTML needed to display the ad image. A new ad is displayed every time the user opens or refreshes the web page.

The Browser Capabilities Component

The Browser Capabilities object enables you to determine the capabilities of a user's browser from within your application. This object is helpful when users with different browsers are accessing your web pages. You can use this object to determine the browser's capabilities and then take the appropriate actions with the application to give the user the best possible experience. The object name of this component is MSWC.BrowserType.

The Browser Capabilities object uses the HTTP User Agent header to determine which browser is being used. This header is sent by the browser when it connects to your web server. Based on the browser's name, this object then identifies the capabilities based on entries in the BrowsCap.INI file. The capability entries in the INI file become properties of the instance of the BrowserType object in your application. After you declare the instance of this object, you can access its properties to determine what capabilities the browser has. For example, you could create an instance of the browser object and then check to see whether the browser supports VBScript, as demonstrated in the following example:

<% Set bc = CreateObject("MSWC.BrowserType")

<% If (bc.vbscript = "True") Then
MsgBox "Your browser supports VBScript."
MsgBox "I can't believe your browser does not support VBScript!"
End if %>

The Database Access Component

You can use the Database Access component to connect to a database from within your application. This component uses the Active Data Object (ADO) model to enable you to connect and interact with the database tables and information. Refer to the lesson on Chapter 8, "Communicating with a Database," for an overview of the ADO model and its objects, methods, properties, and attributes.

The Content Linking Component

The Content Linking component enables you to set up user-friendly page navigation in your application. You can design the flow of your Active Server Pages, include the hierarchy in a text file, and then use this component and its methods to determine the links between the ASPs. The text file lists URL addresses for your application and an optional description for each link. You can create a text file and specify one URL link per line in the order you want to design the page navigation. Once you have created the link file, you can see its contents by using the GetNthURL method, as demonstrated in Listing 17.1.

Listing 17.1. Retrieving the links.

`Declares object instance of the Content Linking component
Set NextLink=Server.CreateObject("MSWC.NextLink")
`Determines how many links are included in the text file
`Loops through the links displaying the URL address and its description
<% For i = 1 to count %>
<li><a href="<%=NextLink.GetNthUrl("/Vroot/Nextlink.txt",i) %>">
<%=NextLink.GetNthDescription("/Vroot/Nextlink.txt",i) %></a>
<% Next %></UL>

The Text Stream Component

The Text Stream component enables you to access the server machine's file system. Its object name is MSWC.TextStream. Using this component, you can perform all file access functions, including opening, reading, writing, saving, and closing a file. This component is useful for storing and accessing information you want to use frequently in your application but don't want to store in a database. You could, for example, store application state information as well as a listing of thoughts of the Chapter.

Creating Custom Active Server Components

In addition to the standard ASP components, you can build your own custom Active Server Components to tailor the component to the needs of your application. As mentioned, Active Server Components are the successors to OLE Automation Servers, which you might have created with Visual Basic 4.0. Server components consist of robust application logic that gives your application functionality without requiring a user interface. You can develop an Active Server Component for your application with a variety of languages and tools, including Visual Basic 4.0 and higher, Visual C++, Visual J++, and other development tools that support building COM components.

The Benefits of Active Server Components

Using Active Server Components in your application offers many benefits. First, the tools that support creating these components enable you to incorporate robust and powerful functionality into your application. You no longer have to supply workaround solutions in your HTML and scripting code to meet the needs of your application. Tools such as Visual Basic and Visual C++ extend your application's capabilities. You can use the power of these tools to process sophisticated logic, thus improving the overall performance of your Web-based application. For example, you could convert the financial calculation routine you created with JavaScript into a powerful Visual Basic or Visual C++ Active Server Component. Your users will appreciate the extra time they gain because of this performance improvement.

Another advantage of Active Server Components is the integration of existing client-server and legacy systems. You may already have existing systems you want to integrate into your new Web-based applications. Active Server Components give you an excellent medium for tapping into the functionality of these applications without having to rewrite entire legacy applications. You can create Active Server Components to serve as the "glue" between your Web-based application and existing systems. The custom component you develop provides a gateway into the existing system's functionality and enables your Web-based application to make use of its capabilities. With custom components, you can preserve the investment in your legacy systems while incorporating new technologies into your applications.

Active Server Components offer another benefit: providing reusability for your development team and other applications. Instead of duplicating similar functions in every application module, you can create an Active Server Component that's accessible from all your ASPs. Your developers benefit because they don't have to code the logic into their ASP module, and you eliminate redundancy in your application, which can improve its performance. Although you may create an Active Server Component with a single application in mind, the component might serve other applications you develop, too. This reusability feature can help reduce your development time.

Integrating Active Server Components and Active Server Pages

This section of toChapter's lesson teaches you how to integrate and use Active Server Components in your Web-based applications. It also offers several examples of using the pre-defined Active Server Components that were previously described.

NOTE: You typically call an Active Server Component from within an ASP. You can, however, invoke an Active Server Component from other Active Server Components, as well as from ISAPI and Java applications.

Creating Dynamic Advertisements

This example uses the Ad Rotator component to demonstrate creating a dynamic web page. The lesson guides you through the process of declaring an instance of this object and then using it within an ASP to display rotating ads on a web page. The GIF files and code used in this example are on the guide's CD-ROM.

You learned how to create and use ASPs in your application on Chapter 11. From the ASP lesson, you know that an ASP enables you to combine both HTML and script to construct a dynamic web page. In a newly created web page, you can use the Ad Rotator component to display alternating advertisements. The first step is to create an instance of the AdRotator object by entering the following line in the ASP file:

<% Set MyAd = Server.CreateObject("MSWC.Adrotator") %>

This code creates an instance of the AdRotator object and sets it equal to the MyAd variable. Once this declaration is made, you can then use the MyAd object in the ASP to access its properties and methods. Another way to create this object is to declare an instance of the object within the Session_OnStart event. Remember, this event begins whenever a user first opens your application. The scope of the object is then established for the entire time the user interacts with the application. The following code example shows how to declare an instance of the AdRotator object in the Session_OnStart event procedure:

Sub Session_OnStart ()
<% Set MyAd = Server.CreateObject("MSWC.Adrotator") %>
End Sub

You can also declare the object as a session object from within the ASP to accomplish the same purpose, as shown in the following code line:

<OBJECT RUNAT=Server SCOPE=Session id=MyAd PROGid="MSWC.Adrotator">

In this example, a page-level instance is used for the AdRotator object. Once an instance of the object has been declared, you can access its properties and methods to make it functional. Use the GetAdvertisement method to retrieve and display advertising images on your web page. Here's the syntax to call this method:


In this code, AdObjectName is the object's instance name, and FileName is the name of the text file containing the image filenames. To retrieve the names of the image files in AdData.txt, for example, you would enter this code:

<% MyAd.GetAdvertisement("AdData.txt") %>

Listing 17.2 shows you the full code example for a sample ASP file that uses the AdRotator object.

Listing 17.2. Using the AdRotator object.


<META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Ad Rotator</TITLE>
<H3>This Page contains dynamic ads!</H3>

`Declares an instance of the Ad Rotator object
Set MyAd = Server.CreateObject("MSWC.Adrotator")
`Writes the necessary logic to display the GIF on the Web page


Based on the logic in this code listing, the advertisements in the AdData.txt file are displayed on the web page, as seen in Figures 17.4 and 17.5.

Figure 17.4.

Viewing the initial ad.

Figure 17.5.

Rotating the ads.

The advertisement images used in this example are some of the same ads used in the Adventure Works sample found on Microsoft's home site and included with Visual InterDev. Listing 17.3 displays the contents of the AdData.txt file.

Listing 17.3. Specifying the ad images.

redirect /AdvWorks/adredir.asp
width 460
height 60
border 1
Astro Mt. Bike Company

Arbor Shoes

Clocktower Sporting Goods


As you can tell from this example, the ads are listed in the text file in the order they will rotate on the web page. The width and height are specified for the ad image container, along with a URL address and description for each of the images. The Redirect statement actually redirects the user back to a web page in the Adventure Works site. If you have installed the sample Adventure Works application, you can run the previous web page example and click an image to go to the Adventure Works site.

Displaying a Browsers Capabilities

This section demonstrates how to use the Browser Capabilities component in an ASP file. The example used in this part of the lesson displays an HTML table listing the capabilities of the user's browser. To use the Browser Capabilities component, you must first declare an instance of the BrowserType object, as shown in the following code line:

<% Set bc = Server.CreateObject("MSWC.BrowserType") %>

You can then use the bc instance of the object to determine and display the browser's capabilities. Listing 17.4 shows the code used to create this table.

Listing 17.4. Creating the Capabilities table.

<% Set bc = Server.CreateObject("MSWC.BrowserType") %>

<TR><TD>Browser</TD><TD><%= bc.browser %></TD></TR>
<TR><TD>Version</TD><TD><%= bc.version %></TD></TR>

<% If (bc.frames = "True") Then %>TRUE
<% Else %>FALSE
<% End If %>


<% If (bc.tables = "True") Then %>TRUE
<% Else %>FALSE
<% End If %>


<% If (bc.BackgroundSounds = "True") Then %>TRUE
<% Else %>FALSE
<% End If %>


<% If (bc.vbscript = "True") Then %>TRUE
<% Else %>FALSE
<% End If %>


<% If (bc.javascript = "True") Then %>TRUE
<% Else %>FALSE
<% End If %>

In this code example, an instance of the BrowserType object is created, and then the object is used to reference the browser's capabilities. A series of checks are made to determine what the browser supports. Based on these checks, the table is formatted with the browser's capabilities for the specified feature. Figure 17.6 displays the results of accessing the ASP with a sample browser.

Figure 17.6.

Discovering the browser's capabilities.

A Call to Action

Active Server Components are crucial to your Web-based applications. As technology continues to develop in this area, developers and users alike are realizing the importance of building a robust architecture for your application. Web-based applications offer a new challenge because the thin client model places even more of a burden on the server. New products, such as the Microsoft Transaction Server and other middleware products, that meet your application's needs are continuing to be developed. As a developer, you must be aware of the challenges in developing robust Internet and intranet applications.

Several years ago, client-server technology lulled developers into thinking they could paint a few screens with a RAD tool and implement a system. Developers soon found out there was more to implementing a system than inserting a few controls on a window and calling the application done. For some developers, it took users screaming about application response time to realize the importance of the system's architecture. Likewise, Web-based applications are now rolling out all over the world.

To avoid making the same mistake they made with client-server technology, developers should become knowledgeable about both the client and server sides of a Web-based application. They should study the application's requirements and design an architecture for it that adequately addresses these needs. Active Server Components can help keep your server from becoming overburdened, so as developers, you should learn more about Active Server Components. Analyze your application and identify whether you need to integrate a component into your application. Next, determine whether you need to build a custom server component and then act accordingly.


ToChapter's lesson should have opened your eyes to the world of Active Server Components. The server side of your Web-based application needs all the help it can get, and Active Server Components are just what you need to do the job. In the future, you will see companies, such as Microsoft, integrating more robust features into their servers to meet the needs of organizations' applications. Active Server Components are just one part of the server equation. Middleware and transaction-processing software will become increasingly important for your Web-based applications, just as they have for client-server technology.

To review what you learned in toChapter's lesson, you got an overview of Microsoft's COM and DCOM technologies. The lesson explained each of these models, their benefits, and how they can be used. Next, you learned about Active Server Components. The lesson gave you a definition, as well as examples of intrinsic and extrinsic ASP objects and components. During the first part of the Chapter's lesson, you gained an appreciation of how Active Server Components serve as the glue between your Web-based application and other new and existing client-server and legacy applications. Next, the lesson demonstrated how to integrate Active Server Components into your application. You learned how to use some of the pre-defined Active Server Components included as part of the ASP model to meet your application's needs. Finally, I urged you to learn more about Active Server Components and how they can be used to develop effective applications.


Q What's the difference between Active Server Pages and Active Server Components?

Active Server Pages combine HTML with client- and server-side script to create dynamic web pages for your application. An ASP can be created with a text editor. Active Server Components, on the other hand, are executable programs or dynamic link libraries that can be called from your ASP. Active Server Components are constructed by using a robust development tool, such as Visual Basic or Visual C++.

Q Can I use Java applications with Active Server Components

The short answer is yes. Java applications are welcome in Microsoft's world of COM components. Using Microsoft's implementation of the Java virtual machine (VM), Java applications appear as COM objects to Active Server Components. Likewise, COM objects appear as Java objects to Java applets and applications. This process is transparent to the components involved because Microsoft's Java VM performs all the necessary translations.


ToChapter's workshop presents a challenge to learn a language or development tool that will enable you to develop a custom Active Server Component. Your choices include Visual Basic, Visual C++, or a Java development tool, such as Symantec's Visual Café. Learn the tool of your choice, and then design and develop an Active Server Component to integrate into a Visual InterDev application. You will be glad you did!


1. Name the five extrinsic components included with the ASP model.

What's the basic difference between COM and DCOM?

What method can be used to retrieve and display rotating advertisement images to the browser?

What's the vehicle or mechanism that enables DCOM objects to communicate with each other?

Quiz Answers

1. Ad Rotator
Browser Capabilities
Content Linking
Database Access
Text Stream

DCOM facilitates the communication of objects located on different machines. COM is used for objects and components residing on the same machine.


Object Remote Procedure Call (ORPC).