ActiveX Server Scripting- A First Look

Rob Ericsson

Interested in the server side of ActiveX scripting-which has been referred to as “Denali”? Here, beta evaluator Rob Ericsson provides you with a practical tutorial.

With all the hype surrounding Microsoft’s ActiveX initiative, it’s sometimes difficult to figure out which parts will be useful in developing robust Internet and intranet applications. So far, most of the emphasis has been on client-side ActiveX components. This article looks at the other side of ActiveX-the server side (see Figure 1).

Figure 1. Microsoft’s basic ActiveX Server framework.

ActiveX Server Scripting (code-named “Denali”) is a powerful and flexible way to create Web applications. It can be used to create dynamic Web applications from scratch or to leverage existing development with a Web front end. It’s open and extensible, allowing use of a wide variety of scripting languages and supporting ActiveX components running on the Web server. In addition, because it doesn’t rely on any particular Web browser capabilities, it offers the widest possible reach, allowing truly dynamic sites to be viewed equally as well with Netscape Navigator or Microsoft’s Internet Explorer (IE).

Anything that can be done with Common Gateway Interface (CGI) programming and standard Hypertext Markup Language (HTML) can be done with ActiveX Server-and then some. ActiveX Server uses OLE Automation Servers and VBScript to provide a rich programming environment and preserve existing OLE development and skill sets. In addition, in many cases an ActiveX Server application will perform better than a similar CGI application because OLE Servers can be run in-process with the Web server, eliminating the overhead (and performance hit) of creating separate processes for each instance of the application.

Overview of ActiveX Server Scripting

An ActiveX Server script consists of VBScript or JavaScript code inserted into a standard HTML file. This code is executed on the server as the script is processed and the results of the code are returned to the browser as standard HTML. Because the scripting is completely integrated with the HTML files, there’s no edit-compile-link cycle and all code (HTML and programmatic) is located in one place. There’s no such bifurcation between writing programmatic code and HTML as there is with CGI script development.

As you might expect, one of the most powerful features of ActiveX Server Scripting is the “tight integration” with OLE Automation Servers. An OLE Automation Server allows you to define classes with associated properties and methods that other applications can manipulate using standard VB syntax. Because OLE Servers can be created using a variety of tools and languages from VB to C to Java, you can use your existing skills to create very advanced functionality without having to learn new tools. Also, the versioning interfaces built into OLE components allow you to seamlessly enhance objects without breaking existing code.

To bind these objects together and create an application requires some type of glue. For ActiveX Server, this means VBScript or JavaScript. Actually, ActiveX supports any kind of scripting language that is written to the ActiveX Script specification. This means that ActiveX Script can, in principle, be extended to any sort of language, such as Perl or REXX, again preserving existing skill sets.

Denali includes the basic components to start developing Web sites immediately. Right out of the box, Denali has objects that facilitate database access, client state management, HTML form processing, file system access, dynamic billboards, browser capability determination, and a content-linking component.

When a client requests a URL with an ASP (Active Server) extension, the Web server calls the ActiveX Server to interpret the file, execute the code, and return the HTML. Because all the processing is done on the server, this precludes the execution of code that presents user interface elements directly. For example, InputBox and MessageBox aren’t supported.

VBScript is a subset of the full-fledged Visual Basic language and there are some important differences. First, VBScript supports only one data type, the Variant. Secondly, for security reasons and for consistency with JavaScript, VBScript has no file I/O (although this capability is provided by an ActiveX object) or direct access to the underlying operating system. Third, the built-in VB constants aren’t available so you’ll have to use the numeric values they represent directly in your code. Overall, though, if you are familiar with VB, you should have no problem coding in VBScript.

An ActiveX Server application consists of a virtual directory on a Web server and all its subdirectories and can be as simple or complex as you want. The special files that define an application must be located in the virtual directory of the application. Each ActiveX Server application can have a file named Global.asa in the root of the application that helps manage the Session and Application objects.

ActiveX Server has a number of built-in objects that facilitate the creation and management of Web applications. The Session object is used to set properties for a single user while that user is in the application. For example, the Session object can be used to customize a site for a particular user’s preferences throughout his or her visit. ActiveX Server automatically creates a session whenever a new user requests a URL that points to the application and is managed through the use of cookies (persistent client-side state-keepers). If cookies aren’t available on the browser, ActiveX Server appends the SessionIDs as a URL Query String parameter. Sessions are terminated if a user hasn’t requested a page in the application during a specified time-out period. Sessions also can be closed by using the Abandon method.

The Application object applies to all users (active sessions) in the Web application. For example, it can be used to send a message to all users. The Request object provides the script with any information passed through with the HTTP request, including all of the standard information and the parameters passed with GET or POST requests. The Response object sends data back to the user via HTTP and HTML. For example, the Write method sends text directly into the HTTP stream and the Redirect method sends the user to a different page. The Server object accesses methods and properties residing on the server itself, including methods to create objects, URL and HTTP encode strings, and to find physical paths for virtual references.

In addition to the basic objects, the ActiveX Server has components that can help you create dynamic pages. These include an advertisement rotator component that rotates through advertisement banners according to a specified schedule, a browser capabilities component which can identify the type of browsers accessing the application, a database access component, a content linking component which creates a table of contents for an application, and a text stream component which allows file input and output. All of these components are interesting and useful, but for the purposes of this article, the one of most interest is the database access component called Active Data Objects (ADO).

ActiveX Server and SQL

ADO is very similar to the familiar RDO in Visual Basic. In ADO, the Connection, Recordset, and Command objects are the primary interfaces to the data (see Figure 2). In a VBScript, a scroll forward, read-only Recordset can be opened with the following code:

Set Conn = Server.CreateObject("ADO.Connection")
Conn.Open "pubs", "sa", ""
Set RS = Conn.Execute("select * from authors")

Figure 2. The RDO Object Model.

Fully scrollable and updateable cursors are only slightly more difficult to create. ADO also supports transactions, bookmarks, and BLOBS, making it possible to add fairly complex database interactions to a Web site.

In many ways, developing an ActiveX Server application is like developing a typical VB client/server application. The object models are similar and the language (VBScript) is a subset of VB. Thus, it becomes easy to leverage existing VB and SQL Server expertise onto the Web. The only big differences are that ActiveX Server Scripting has no Integrated Development Environment (yet) and there’s another language in the mix-HTML.

An example

An application I’ve developed using ActiveX Server is the prospect analysis component for the fine.com site at http://www.fine.com. It utilizes many of the components of ActiveX Server mentioned in this article. Because the ActiveX Server Scripting was still in beta when the application was constructed, some of this code may be superseded when the final product is released. However, it should give you a pretty good idea of how to implement a dynamic Web site using ActiveX Server.

The basic idea behind this application is to collect information about potential clients without presenting a forbidding form with 100 questions. Instead, site visitors can answer contextually appropriate questions as they travel through the site.

As described previously, the Global.asa file creates the Session object and any other objects or variables necessary for the application (in this case, an ADO object and a visID variable). It also destroys those objects when the session expires:

Global.asa
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
 SUB Session_OnStart
 REM This script executes when the ActiveX server 
 REM creates a new session prior to executing any 
 REM requested page.
  REM Open ADO Connect
  Set OBJfine_info = Server.CreateObject("ADO.Connection")
  OBJfine_info.Open "fineSite", "webuser", ""
  Session("Conn") = OBJfine_info
  REM Check to see if new client
  visID = Request.Cookies("finevisitor")
  If visID = "" Then
  	'add them to the database
	Set RS = OBJfine_info.Execute("spUpdVisitor")
	' get their ID
	visID = RS("VisitorID")
	'add a cookie to their browser
	Response.Cookies("finevisitor") = visID
	Response.Cookies("finevisitor").Expires = "Feb 1,1999"  
  End If
  Session("visID") = visID
 END SUB
</SCRIPT>
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
 SUB Session_OnEnd
 REM Is there anything we want to do when the 
 REM session ends?
 REM Close the connection.
 OBJfine_info.Close
 END SUB
</script>

Each page in the site resides in a borderless frameset that is constructed by the following HTML:

<html>
<head>
<title>fine.com</title>
</head>
<frameset rows="42,227,*" framespacing="0" frameborder="0">
 <frame name="top"  src="elevator-t.htm" scrolling="no" 
     marginwidth="1" marginheight="1">
 <frame name="main" src="elevator-m.asp" scrolling=
     "auto" marginwidth="1" marginheight="1">
 <frame name="bottom" src="blank.htm" scrolling=
        "auto" marginwidth="1" marginheight="1">
</frameset>
</html>
</body>
</html> 

The page content is embedded in ASP files like this one and is loaded into the middle frame. The code ensures that an ADO connection is available, logs the page visit to the database, and then creates a JavaScript in the page header to load the appropriate files into the top and bottom frames:

<%
'define the page name
pageName = "exec"
'set the page name cookie
Response.Cookies("finepage") = pageName
'get the visID
visID = Session("visID")
'check to see if the ADO connection is active
If IsObject(Session("Conn")) Then
	Set C = Session("Conn")
Else
	Set C = Server.CreateObject("ADO.Connection")
	C.Open "fineSite", "webuser", ""
	Session("Conn") = C
End If
'log this page hit to the database by calling 
'a stored proc
strSQLLogVisit = "exec spRecordVisit " & visID & ", '" & _
	pageName & "'"
C.Execute(strSQLLogVisit)

%><html>
<head>
<SCRIPT>
	open('question-b.asp', 'bottom');
	open('<% =pageName %>-t.htm', 'top');
</script>
</head>
<body bgcolor="#000000" link="red" vlink="grey" 
     alink="white" text="ffffff"> 
<font face="arial">
<center>
<img border="0" usemap="#exec" src="images/exec.jpg" alt=
     "Welcome to the Excutive Offices">
</center>
<map name="exec">
<area shape="rect" coords="0,0,84,214" href="" target=
     "main">
<area shape="rect" coords="0,103,169,214" href=
     "exec-media_resource.htm" target="_top">
<area shape="rect" coords="245,15,374,183" href=
     "exec-pptindex.htm" target="_top">
<area shape="rect" coords="423,23,490,163" href=
     "elevator-m.asp" target="main">
<area shape="rect" coords="546,10,611,208" href=
     "" target="main">
<area shape="rect" coords="633,6,716,214" href=
     "" target="main">
</map>
</body>
</html>

The questions appear in the bottom frame and are generated by the following ASP file:

<%
'get the page name
pageName = Request.Cookies("finepage")
'get the visID
visID = Session("visID")
Ômake sure an ADO connection is established
If IsObject(Session("Conn")) Then
	Set C = Session("Conn")
Else
	Set C = Server.CreateObject("ADO.Connection")
	C.Open "fineSite", "fineuser", "malarkey"
	Session("Conn") = C
End If
  	'use spGetNextQuestion to return next question id
	Set oQRS = Server.CreateObject("ADO.RecordSet")
	'get the next question id
	szConnString = "spGetNextQuestion '" &pageName &"',_
      " & visID
	'open a connection on C with a static cursor
'cursor membership is fixed and data is a snapshot.
	oQRS.Open szConnString, C, 3
	'get the first column returned, the id
iQID = oQRS(0)
'close the record set
	oQRS.Close
	'get the question text
	szConnString = "select Question from tbQuestion where _
     ID = " & iQID
	'open a connection on C with a static cursor
'cursor membership is fixed and data is a snapshot.
	oQRS.Open szConnString, C, 3
	'get the first column returned, the question text
	szQuestion = oQRS(0)
	'close the record set
	oQRS.Close
	'write the body tag
%>
	<html>
	<body bgcolor="#000000" text="#ffffff">
<% if szQuestion <> "*None" Then %>
	<form method="post" action="putAnswer.idc">
	<input type="hidden" name="qid" value="<% =
       iQID %>">
	<input type="hidden" name="vid" value="<% =
       visID %>">
	<input type="hidden" name="page" value="<% =
       pageName %>">
	<p><% =szQuestion %>
	<p><input type="submit">
	</form>
<% end if %>
</body>
</html>

All the questions are stored as HTML in a text field in the database, making it very easy to update the questions as required. All the answers are posted using the Internet Database Connector (IDC), although the ASP file could be modified easily to also update the database.

Conclusion

ActiveX Server offers many advantages over existing Web programming tools. Its use of VBScript and OLE Automation Servers preserves much existing investment in both code and expertise. This translates to better, cheaper, and faster development cycles, which you need to keep up with the fast pace of change on the Internet today.

Because ActiveX Server Scripting is a server-side tool, it preserves all the platform independence of regular HTML without furthering the Balkanization of the Web. Also, Microsoft’s use of Denali to provide customized and dynamic Web content on some very high-profile sites, including MSN (www.msn.com) and MSNBC (www.msnbc.com), bodes well for its performance and scalability.

All in all, ActiveX Server represents a positive step in the evolution of tools using data from databases to present information on the Web. Anyone with an investment in VB programming skills and a desire to get their data on the Web should give ActiveX Server serious consideration. s

Rob Ericsson is a database analyst at fine.com Interactive, one of the Northwest’s best Web site developers. Rob is working toward Microsoft Certified Solution Developer status. His recent focus has been the creation of dynamic Web sites using SQL Server, Visual Basic, and Internet Information Server. rob@fine.com.

To find out more about SQL Server Professional and Pinnacle Publishing,
visit their website at
http://www.pinpub.com/sqlpro/

Note: This is not a Microsoft Corporation website.
Microsoft is not responsible for its content.

This article is reproduced from the December 1996 issue of SQL Server Professional. Copyright 1996, by Pinnacle Publishing, Inc., unless otherwise noted. All rights are reserved. SQL Server Professional is an independently produced publication of Pinnacle Publishing, Inc. No part of this article may be used or reproduced in any fashion (except in brief quotations used in critical articles and reviews) without prior consent of Pinnacle Publishing, Inc. To contact Pinnacle Publishing, Inc., please call (800)788-1900 or (206)251-1900.