Image of Navigational Map linked to Home / Contents / Search Exploring the VB6 WebClasses

by Matt Gray - GUI Computing
Image of Line Break

Before I started the research for this article, the new VB 6 WebClasses were a mystery to me. So those looking for a high-powered guru-level expose of the minutiae of this really cool component of VB6 should look away now. For those still with me, read on and hopefully we'll both get an insight into the WebClasses and their features.

Pre VB6, the only way to create an IIS application was to write ASP which utilised components that you wrote using VB, Delphi or other languages that can create COM DLLs. (See Jim Karabatsos' article, VB6 - A first look in our last issue). This is all very well if you don't mind using a bit of VBScript and a bit of Visual Basic (and perhaps a bit of Delphi) for the same piece of development. Personally, not quite my scene.

The WebClass changes all that by enabling you to write VB6 code wrapped up in an ActiveX DLL (which resides on the server) that has access to the HTTP requests from the browser. This means you can do everything you can do (and more) in ASP in VB6 code - and work in the VB6 IDE, which is a huge bonus. Working this way also means you don't need to know ASP (and a lot of people don't want to) because an IIS Application creates only one ASP page that hosts the WebClass (and you don't need to edit that unless you're crazy!). After that, everything is all fresh and lovely VB (as if that doesn't open up a world of possibilities…)

The very nature of an IIS Application moves even more towards the world of components and can completely separate the User Interface from the internals of the application.

The whole UI can be produced by HTML designers/graphics designers along with named tags in the HTML (which dynamic data will later replace). The WebClass processes these tags in an event and produces any dynamic HTML you like to replace the tags as it writes out the HTML template. However, WebClasses are not limited to producing pure HTML. They can return any valid MIME type, including such things as Dynamic HTML, and XML.

IIS Applications developed this way are also more like Standard VB Applications when you observe the general life-cycle:
VB App : Create the forms, write the code, debug and test, package and deploy
IIS App: Create html, write the code, debug and test, package and deploy

This is probably sounding better to some people who want to get away from this:
Create HTML, edit HTML, recreate ASP pages for HTML, write VB code (DLLs or other components), debug and test, write ASP code, 'debug' and test, package and deploy - phew!

So what is a WebClass again?

A WebClass is a Visual Basic-created COM DLL that resides within Internet Information Server (IIS). A WebClass allows you to create server-side applications that perform all of their processing on the Web server, serving up HTML pages as the user interface, which can then be viewed in any browser.

Basically, when compiled, the WebClass generates an associated ASP page that is used to host the WebClass on the Server. This ASP basically just passes the WebClass its Application, Request, Response, Server and Session objects so that VB can utilise these objects in code (It should be noted that it would be fairly easy to set this up yourself and have your own WebClass, but VB6 WebClass' do have a lot more to offer than just ASP objects (which we will see later).

The WebClass lifecycle

Initialise, BeginRequest, EndRequest , Start, Terminate are all key events in the life of a webclass. As the names suggest, these may be obviously compared to a form-based application's Load and Unload events. An IIS Applications's BeginRequest or EndRequest events can perform some of the same functions as the Load and Unload event did - these events can be used to initialize values your application needs to run, and clean up state and other values at the end of a request. Begin and EndRequest are unique events for a webclass. A webclass may receive multiple requests throughout its life. These events allow the webclass to process each request as a unique entity. The Start event generally occurs the first time BeginRequest is fired in the application and the Terminate event tells the run-time DLL to destroy the instance of the webclass.

Note : If the StateManagement property for the webclass is set to wcNoState, the webclass fires the Terminate event each time it sends a response to the browser. If the StateManagement property for the webclass is set to wcRetainInstance, the webclass fires the Terminate event only after it calls the ReleaseInstance method. You'll mostly be working in the latter mode, in my experience.

What's in a WebClass?

WebClasses contain one or more WebItems, which can be one of two things :

Templates (HTML Template file) - An HTML file that you associate with your webclass which usually contain special tags where data can be customised via the VB code in the WebClass

Custom WebItems are WebItems that you build that aren't associated with a HTML page, rather they may be used to generate a generic response to the browser. You can also build custom events here which might be called either when the page loads or when a user selects an HTML element (ie. You can resource your events to do something like fill a standard header for pages read from a database).

WebClass Events A WebClass can have three different types of events :

Useful properties and methods

These are the most common and useful properties and methods you will use in your app :

State Management

So how does one go about State management now we are using WebClasses?

There are several ways you can store state for a Web-based application:

You can also use a combination of these methods. It is also important to note again here the WebClass' StateManagement property I described above.

A simple WebClass example

Thinking this is all a bit weird yet? Well, here is a small example that might put you in the right direction - this is getting tricky to explain, but is actually quite easy to code.

This little app is just outputs all the ServerVariables in an HTML table, so it's very basic. To build it you'll need:

1. Create a HTML template with an appropriate HTML editor. To dynamically insert data e.g. Tables, or any HTML via your VB code simply place tags within your HTML where you want the data inserted and prefix the tags with WC@ (NB: You can change this tag prefix in your WebClass) :

	<WC@ServerVars>Server vars table goes here</WC@ServerVars>

Here is the HTML used for this example:

<html>

<head>
<title>WebClasses Server Variables Example</title>
</head>

<body bgcolor="#c1a27d">
<center><H3>Server Variables</H3></center>

<p align="center"><WC@SERVERVARS>Insert 
Server vars table Here</WC@SERVERVARS></p>
</body>
</html>

Later in your WebClass you can write code to replace this tag for your actual table

2. Start a new IIS Application and name your project WCExample

3. Name your WebClass 'clsExample' and label your NameInUrl property to 'default' (this will be the starting page of your app - the one ASP page generated to host your WebClass). NB: For more information on the WebClass designer see the VB6 help file.

4. Save your project.

5. Add your HTML Template file to the WebClass Designer - do this by either right clicking on the class name in the Designer or on the web page icon. Rename the template to ServerVars.

6. Get rid of the code in WebClass_Start() and replace it with:

Set WebClass.NextItem = WebClass.ServerVars

This will tell the WebClass to start the app with the ServerVars template.

In the ServerVars_Respond() event insert the code:

ServerVars.WriteTemplate

This tells the webclass to output the template and replace tags as appropriate.

7. To dynamically generate your Server variables table you should now place code in the serverVars_ProcessTag event. This event gets passed these parameters which help you generate the 'TagContents' by filling the TagContents parameters:

8. Copy the following code into the ServerVars_ProcessTag event:

Dim name As Variant

Select Case TagName
	Case "WC@ServerVars"
	 TagContents = "<TABLE BORDER=1>"
	 TagContents = TagContents & "<TR><TD><b>Server Variables:</b></TD>"
	 TagContents = TagContents & "<TD><b>Value</b></TD></TR>"
	 For Each name In Request.ServerVariables
	   TagContents = TagContents & "<TR><TD>" & name & "</TD>"
	   TagContents = TagContents & "<TD>" & Request.ServerVariables(name)          
	   TagContents = TagContents & "</TD></TR>"
	 Next
	 TagContents = TagContents & "</TABLE>"
	Case Else
	 'no other tags
	 TagContents = "No tags found"
End Select
	  

This example shows the use of the ASP Request object used in this little site.

9. Compile your DLL. You should notice the ASP page generated (name with your NameInUrl property) and your project DLL now exist in your project directory. The webclass's run-time component (MSWCRUN.DLL) is created when the application is first started and launches the first event in the webclass's life cycle.

10. Point your browser to your ASP page under your alias on the server and you should see something like the following:

Now that is just a simple example, but hopefully it gives you some of the flavour of how an IIS Application created this way works.

In Summary

There are a lot of plusses involved in building IIS Applications this way. It certainly beats Perl and CGI programming hands down. However, like any new technology, the WebClass approach can take some time to get comfortable with. It is probably a biased opinion, but I would rather use ASP and VB combined for now. For one thing, I'm comfortable in that combined environment, for another I'm not certain that having absolutely everything in a server-side DLL is always the best option (although the use of HTML templates certainly makes that much less of an issue than it was).

But for those who don't want to go near scripting with a ten foot pole... there's a very good chance that WebClasses might suit you.



Written by: Matthew Gray
October '98

Image of Arrow linked to Previous Article
Image of Line Break
[HOME] [TABLE OF CONTENTS] [SEARCH]