Monday, 08 September 2008
PicoSearch

Back to school for WebClasses

This column appeared in the June 1999 edition of EXE, and attempts to provide an overview of internet programming issues with particular reference to WebClasses - in 2000 words!! Not easy...


This month I’m taking a look at the new WebClass technology. Before I start I’d just like to make an apology to a certain subset of the readership who might regard the contents of this months column as being rather fundamental. However I do know that the great majority of Visual Basic developers are still focussed solely on conventional Windows applications development. Many such folk are interested in Internet development issues – if only for the sake of their CVs – but have yet to get started. Therefore for the next couple of months we’ll be taking a look at what WebClasses actually are, and of necessity we’ll also be having a look at Microsoft Internet Information Server (IIS). I must emphasise that due to the size of this column I can only initially provide the very basic elements with which to get running. Further reading will be essential to make any headway with this topic. Initially, I’m working on the fairly safe assumption that the reader will be familiar with actually using a web browser. I also need to show some lightweight HTML.

So what are WebClasses then, and why do we need them? In a sentence, they are ActiveX DLLs that run on a web server. However, to understand them we can start by taking a look at the simple internet model. The act of browsing an internet page involves being sent a stream of HTML from a web site. Each web page is stored on the server as a separate HTML file, produced either by hand or by a generator such as Microsoft FrontPage. An HTML page is a set of formatting instructions very similar to the more familiar Rich Text Format (RTF). Although HTML itself doesn’t support any programming constructs such as If..Else..EndIf, the typical web browser does support embedded scripts through a <SCRIPT> tag. The most common of these client-side script languages are VBScript and JavaScript (JavaScript is a language defined by Netscape Communications, Internet Explorer supports a Microsoft version of this called JScript).

This method of sending raw script down the wire is fine as far as it goes, but drawbacks start becoming apparent as a web site becomes more ambitious. For example all of your business logic must be sent to the user as clear text. Apart from giving away information that you would rather keep to yourself, this can also have an impact upon performance because potentially large scripts might need to be downloaded and run simply in order to get a single line of text as an answer to a user query. This problem is further compounded by database access requirements.

ASP Object Model

WebClasses are designed to produce web applications that can be run from any browser on account of the fact that all of the processing code is run at the server end. To make this work the server side of the operation must support the Active Server Page (ASP) programming model. In a production-level model this equates to Internet Information Server running under Windows NT Server. However for development purposes this can also mean either the Peer Web Services if you’re running Windows NT Workstation (found on the Windows NT Option Pack CD), or the Personal Web Server for Windows 95/98.

IIS exposes the Internet Server API (ISAPI). This is a standard means of access for a web developer to produce code that interacts directly with the server service. However, being an API means that it is more tailored for C++ programmers. Therefore Microsoft have also designed the ASP model as an extension to the ISAPI feature set, which offers a scripting environment designed to accommodate server–side execution. This model is now further extended because Visual Basic 6.0 provides a programming interface that mirrors it. To understand the nature of the object model it is necessary to review the basic process flow that occurs when downloading an internet page: once a browser has established an initial TCP/IP connection to a server it then sends a request message. This two-stage action normally comes about through an event such as clicking a hyperlink or supplying an address in the navigation bar. The server then generates a response and passes it back to the caller. This process is mapped into Visual Basic programming terms through the provision of a Request object and a Response object. These objects are predefined and are globally available within the DLL, similar to the globally-available Forms collection within a conventional VB application.

The Request object contains several collections of information that the server will need to know about, the most important of which is the QueryString collection. This holds any specific pieces of data that the web page has passed up along with the request, such as any data that the user has supplied (for example, a search string). Another of these collections is called ServerVariables which stores data of a more environmental nature, such as the screen resolution of the browser.

The Response object is concerned with sending data back to the browser. This return data can either be written directly to the caller via the Write method, or it can be gathered together into a buffer and sent later via a call of the Flush method.

Using either of these methods can result in a full web page being created on the fly, for example:

With Response
  .Write "<html>"
  .Write "<body>"
  .Write "My web site is coming soon. Please call again.."
  .Write "</body>"
  .Write "</html>"
End With

Alternatively the object also contains a Redirect method which points the browser to a different location than was originally requested.

In addition to these two objects, there are also another three that complete the family:

The Application object, for which a single instance exists during the lifetime of the web server.

The Session object, which exists for the time that each user is connected to a site provided by the web server. Together with the Application object, this is useful for storing state information. I shall be providing greater coverage of this topic in next months column.

The Server object, which provides the ability to create ActiveX object instances.

Creating a new application

WebClass development is different from conventional applications development in that you do not have a form painter. When you are defining the individual components of a WebClass – called WebItems – you need to have an HTML template already defined. However you go about this depends upon what you feel the most comfortable with. If you are really new at web development but you are keen to roll your sleeves up then you can buy yourself an HTML manual and type the code in via a Notepad session. However, while you’re busy figuring out what the <OL> tag stands for the rest of us will probably want to use something like FrontPage or Visual InterDev to construct our web pages. The Advanced tab of the Tools, Options menu allows your choice of the web page editor to be set, which by default is Notepad.

Actually this approach to importing existing HTML files can lead to a much more clearly defined division of labour. With conventional Windows application development the form design is often performed by the same person that writes the underlying code (ignoring any concepts of tiered development for the sake of this discussion). In the case of a WebClass project the actual template could be constructed by a specialist in graphics design, leaving the coding side of the operation to be performed by a software developer. At least, that’s how Microsoft sell it. In the real world developers often prefer to work on their own creations from start to finish.

When you load up a new Visual Basic session you need to choose the ‘IIS Application’ project type. Once the environment has duly created itself you will find that you have a Designers folder within the project window with a default WebClass1 entry already assigned. At this stage it’s worth having a quick look in the Project, References dialog to see what additional files have also been pulled in. The Microsoft WebClass Library v1.0, MSWCRUN.DLL (i.e. MicroSoft WebClass RUNtime), is the redistributable file that maps the Visual Basic calls into the Active Server Pages environment. The other new reference, the Microsoft Active Server Pages Object Library, or asp.dll for short, is the destination for all browser requests for targets that have an ‘.asp’ extension.

Having created the project you need to save it before you can do anything with it. I would suggest that you save your project to a local drive because you can immediately find yourself getting dialogs asking for passwords for shared network resources, which in the early stages can be quite intimidating and could easily result in your not looking at internet development for another 18 months. Now you can import a template into the project, via the Add HTML Template WebItem button on the toolbar. I have created a very simple HTML file called thankyou.htm, the contents of which are shown in Listing 1.

<html>
<head>
<h1>EXE Subscriptions Department</h1>
<p>Thank you for visiting our site.</p>
</body>
</html>

Listing 1: Contents of Thankyou.htm

When you import a template file into your project the WebClass designer actually makes a copy of it and uses this instead. Therefore my source file of Thankyou.htm now also exists as Thankyou1.htm. In my example I renamed the default WebItem name of Template1 to Thankyou for consistency with the intended functionality of the form. The WebClass designer, together with the newly added WebItem, can be seen in Figure 1.

Figure 1: WebClass designer form

Double clicking the WebClass1 entry in the left hand pane of the WebClass designer brings up the associated code window. It can be seen that code for a basic response has been generated by default. In order to start up with a web page of our own we can replace the whole text as follows:

Private Sub WebClass_Start()
Set NextItem = Thankyou
End Sub

The purpose of the NextItem property is to shift the context to a different WebItem. In order to cause the WebClass to actually process the template and pass the contents out to the browser it is then necessary to call the WriteTemplate method for the Thankyou WebItem’s Respond event:

Private Sub Thankyou_Respond()
Thankyou.WriteTemplate
End Sub

From the outset it should be possible to compile the project into a DLL. Looking at the contents of the project directory reveals that another file, in this case called webclass1.asp because I’ve not renamed it, has also been created. The contents of this file can be seen in Listing 2.

<%
Response.Buffer=True
Response.Expires=0
If (VarType(Application("~WC~WebClassManager")) = 0) Then
  Application.Lock
  If (VarType(Application("~WC~WebClassManager")) = 0) Then
    Set Application("~WC~WebClassManager") = _
    Server.CreateObject("WebClassRuntime.WebClassManager")
  End If
  Application.UnLock
End If

Application("~WC~WebClassManager").ProcessNoStateWebClass _
  "Project1.WebClass1", Server, Application, Session, _
  Request, Response
%>

Listing 2: Contents of WebClass1.asp

This is the code that is actually fired first when the server is accessed. After initialising a new instance of the WebClass runtime environment it then sets the Application context to Project1.WebClass1. Once the DLL has been properly installed onto a production server all of these details will of course have been added to the registry so the Project1.dll file will be called to provide the WebClass1 code. This sequence shows that an ASP file exists in a one-to-one relationship with a WebClass.

What I’ve covered so far are the very basics of WebClasses. For existing Visual Basic developers there are some new concepts to come to terms with, but at the end of the day much of it is broadly similar to existing principles. Having now made a start with this subject, I intend to continue WebClass coverage next month by taking a look at some of the development issues in more detail.

Click here to go to follow-up article

Copyright ©2002 Jon Perkins I, Jon Michael Perkins, hereby assert and give notice of my right under section 77 of the Copyright, Designs, and Patents Act 1988 to be identified as the author of the foregoing article.