

ASP ADVANCED
This chapter introduces the
first of the ActiveX components that are included with Active Server
Pages. The first section
provides an overview of the methods of integrating components into your
Active Server Pages. In the
next section, you learn how to use the Browser Capabilities component.
Finally, in the last section, a sample application of the Browser
Capabilities component is provided.
Using Components in
Active Server Pages
Previous chapters explained
how to use the built-in Active Server Pages objects, such as the Request
and Response objects. Active
Server Pages components are very similar to these objects.
However, a component isn't as tightly integrated with Active Server
Pages. Components are
intended to extend the core functions provided by the built-in objects.
You can create components
of your own, using such languages as Visual Basic, C++, Java, and Delphi.
You learn how to do this in Chapter , "Extending Active Server
Pages." You can also buy components from third-party companies.
For example, the company Software FXsells components that allow you
to generate charts and graphs easily with your Active Server Pages (see
http://www.softwarefx.com). Finally,
Microsoft bundles a number of free ActiveX components with Active Server
Pages. You learn how to use
these bundled components in this chapter and the following chapters.
Before you can use a
component, you must first create an instance of it.
You can automatically access the properties, collections, and
methods of the built-in objects on every page.
To use a component, on the other hand, you must create an instance
of the component with a particular scope.
In the following three sections, you learn how to create an
instance of a component with page, session, and application scope.
Creating a Component
with Page Scope
In most cases, you'll
create an instance of a component with page scope.
A component with page scope is created on a single page and dies
when processing on the page ends. You
can't use a component with page scope on any page where it wasn't
explicitly created. To create
an instance of a component with page scope, you use the
server.CreateObject( ) method.
Here's an example of
creating a component with page scope:
<%
Set MyBrow=Server.Create0bject("MSWC.BrowserType")
%>
This script creates an
instance of the Browser Capabilities component.
It assigns the variable MyBrow to an instance of this component.
Notice the use of the VBScript set statement.
Because you're assigning an instance of a component to a variable,
you must use the set statement.
The method for creating an
instance of a component with page scope using JScript is very similar.
However, you must use the var statement rather than the set
statement.
The following example
uses the var statement:
<%
var MyBrow=Server.CreateObject("MSWC.BrowserType")
%>
Microsoft recommends that
you create the majority of your components with page scope.
By creating components with page scope, you place less of a burden
on the Web server. A page
scope component releases any memory and other resources it requires when
the processing of the page comes to an end.
Creating Components
with Session Scope
Two methods are available
for creating components with session scope.
One method is to assign a component to a session variable by using
the server.CreateObject( ) method, as in the following example:
<%
Set Session("MyBrow")=Server.CreateObject("MSWC.BrowserType")
%>
This script assigns the
session variable named MyBrow to an instance of the Browser Capabilities
component. The session
variable is available on every page that a particular user requests.
You can place this script in the Session_OnStart script of the
Global.asa file or in any other Active Server Page.
This method of creating a
component with session scope has a significant disadvantage.
The Server.CreateObject( ) method immediately creates an instance
of a component. Even if the
instance of the component is never used, it still drains resources from
the server.
Fortunately, there's
a second way to create a component with session scope. You
can create a component within the Global.asa file by using the Microsoft
HTML <OBJECT> tag, like this:
<OBJECT RUNAT="Server"
SCOPE="Session" ID="MyBrow"
PROGID="MSWC.BrowserType"></OBJECT>
This example shows how to
create an instance of the Browser Capabilities component with the
<OBJECT> HTML tag. The
SCOPE attribute indicates that the component created should have session
scope. The ID attribute
provides the component with a unique identifier (a name), so you can refer
to it in your Active Server Pages scripts.
The PROGID is used to specify the component's registered name.
This is the name the server uses to identify the component when it
creates an instance of it. It's
the same name you would pass to the Server.CreateObject( ) method.
When you use the
<OBJECT> tag in the Global.asa file, you must place it outside any
of the scripts. Do not use
the <OBJECT> tag within the session_OnStart, Session_OnEnd,
Application_OnStart, or Application_OnEnd script.
When a component is created
with session scope in either of the two ways just described, any of its
methods, collections, or properties are available on any page that a
particular user requests. However,
a particular instance of the component must be created for each user.
Like a session variable, a component with session scope is created
relative to a particular user session.
When would you need to
create a component with session scope?
In Chapter 21, "Working with Advertisements," you learn
how to use the Ad Rotator component.
The Ad Rotator component can be used to display different banner
advertisements with different frequencies.
If you want to display banner advertisements on a number of pages,
it would make sense to assign the Ad Rotator component to a session
variable.
Creating
Components With Application Scope
When
you create an instance of a component with application scope, you
can treat it as if it were a built-in object.
Once created, any methods, collections, or properties of the
component can be accessed by any user on any page.
The component remains available until the server shuts down, the
Global.asa file is modified, or the application is unloaded.
You
can create a component with application scope by using methods similar to
those used to create a component with session scope.
First, you can create a component with application
scope by using
the server.CreateObject( ) method. Look
at this example:
<%
Set Application("MyBrow")=Server.CreateObject("MSWC.BrowserType")
%>
Here,
the Browser Capabilities component is assigned to an application variable.
You can do this inside a script within the Global. asa file, such
as the Application_OnStart( ) script. You
could also create a component in this way within any of your Active Server
Pages. After an
instance of the Browser Capabilities
component has been created with application scope, you can use its
properties on any Active Server Page.
You
can also create a component with application scope by using the Microsoft
HTML <OBJECT> tag, like this:
<OBJECT
RUNAT="Server" SCOPE="Application" ID-"MyBrow"
PROGID="MSWC.BrowserType"></OBJECT>
In
this example, the <OBJECT> tag is used to create an instance of the
Browser Capabilities component with application scope.
The SCOPE attribute indicates that the component should have
application rather than session scope.
The ID attribute provides a name for the component.
The PROGID attribute allows the server to identify the component.
You
can place the <OBJECT> tag within the Global.asa file.
However, it must be located outside of any of the scripts.
Don't place the <OBJECT> tag within the Session_OnStart,
Session_OnEnd, Application_OnStart, or Application_OnEnd script.
When
would you need to create an object with application scope?
In the WhosOn Page programming example in Chapter 17, "Working
with Active Server Pages Applications," you learned
how to track user page requests. This
information was stored in a dictionary created with application
scope. This component needed
to be created with application scope.
Otherwise, it could not be accessed by
every user on every page.
|