Page #32 (Chapter 4 - Introduction to WebClasses)

Chapter 4 - Introduction to WebClasses

Visual Basic Developers Guide to ASP and IIS
A. Russell Jones
  Copyright 1999 SYBEX Inc.

Understanding How WebClasses Work
In their first implementation, WebClasses don't talk directly to the server. When you create an ActiveX DLL in Visual Basic, you typically also create another project that instantiates the objects that your DLL exposes, so that you can test them. Similarly, a WebClass is a DLL that needs to be loaded; the objects it exposes need to be instantiated from another program.
At this time, Microsoft has chosen to let WebClasses work through an intermediary ASP page. The browser requests the ASP page from the server, which starts the ASP engine. The ASP engine parses the ASP page and executes the code, which instantiates the WebClass. The WebClass typically gets HTML from an HTML template and retrieves data from a database. It inserts the data into the HTML template and returns the resulting HTML page, possibly with embedded client-side script, to the browser. You can see this process in Figure 4.1.
Here's a closer look at what happens in the WebClass request cycle: The user types the URL of an ASP page into the browser. For this example, assume the ASP page's URL is http://myserver/WebClass1.asp. The ASP page does nothing but instantiate a WebClass object. Listing 4.1 shows the code from a VB-generated ASP page that creates a WebClass.
Listing 4.1: VB-generated ASP Page to Display a WebClass
<%
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").ProcessRetainInstanceWebClass _
     "Project1.WebClassTest", _
          "~WC~Project1.WebClassTest", _
          Server, _
          Application, _
          Session, _
          Request, _
          Response
%>
You've already seen information about the Response.Buffer call. The line Response.Expires=0 tells the browser that the page expires immediately—the next time the user requests this page, the browser should request it again from the server, not from the browser's cache.
The If…End If block checks the Application object variable called ~WC~WebClassManager to see whether it's empty (a VarType function return of 0 indicates an empty variable). If so, it locks the Application object. The code then immediately checks again to see whether the variable is empty. Why? Because you're now working in a multiuser environment. It's possible that another thread set the application variable object reference sometime after the first check returned zero but before the Application.Lock command executed.
  Note You cannot assume that your session is the only one running. You must either know or perform the appropriate checks. In the preceding example, if a reference already exists and you suddenly overwrite the variable with a new reference, some other user will have problems.
If the variable is still empty, the code tells the Server object to create a new instance of an object called a WebClassManager. You don't need to know anything about this object other than that it manages multiple instances of WebClasses for you, so that many people can use your WebClass simultaneously in an application.
Finally, the code calls either the ProcessRetainInstanceWebClass or Process-NoStateInstance method of the WebClassManager object (you'll see more about these two methods later). It passes two string references—the project and name identifier of your WebClass as found in the Registry, and a WebClassSessionName string. It also passes references to most of the ASP objects, so that your WebClass can use the properties and methods of those objects to communicate with the server and return content to the browser.
Now you've seen the first difference between a standard class and a WebClass. VB ensures that a WebClass automatically gets a reference to the ASP objects on start-up. In contrast, here's how you would instantiate a VB5 ActiveX DLL from an ASP page:
Dim myObj
Set myObj = Server.CreateObject("MyProject.MyObject")
At this point, you could either explicitly pass references to the ASP objects or you could write OnStartPage and OnEndPage methods in your DLL, which the server automatically calls immediately after creating the object. When the server calls the OnStartPage method, it also passes a reference to the ASP ScriptingContext object as a parameter. The ScriptingContext object exposes methods that let your DLL obtain references to the ASP objects.
At any rate, when the ASP engine first creates the WebClass, it will fire the WebClass_Initialize event. The WebClass_Initialize event lets you perform initialization for that WebClass.
Next, the WebClass will fire one of two events. If the user made the request without asking for a specific WebItem—for example, the user  typed only http://myserver/WebClass1.asp into the browser—the WebClass will fire the WebClass_Start event. If the user requested a specific item, the WebClass will fire the Respond event for the requested item. Figure 4.2 shows the complete sequence of events.
The WebClass_Start event lets you select which WebItem will be processed first as well as perform any other initialization for this specific instance of the WebClass.
The Respond event fires for the requested item. This is important. The requested item is typically an HTML template or a custom WebItem, so the event code has a different name for each item, for example, Template1_Respond.
Finally, the ProcessTag event—again, an item-specific event—provides you the opportunity to replace WebClass-specific tags with whatever you wish: HTML markup, content, script, input controls, etc.
  Warning The mswcrun.dll file that comes with Visual Studio Service Pack 3 (sp3) has some serious problems with tag replacement. When running in design mode, the WebClass truncates the HTML returned to the browser. This behavior does not occur at runtime. To solve this problem, you need to replace the sp3 version of the DLL with an earlier version. For more information, see the Microsoft Knowledge Base article Q234317.



Visual Basic Developer[ap]s Guide to ASP and IIS
Visual Basic Developer[ap]s Guide to ASP and IIS
ISBN: 782125573
EAN: N/A
Year: 2005
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net