How to Create ASP Applications Without Writing ASP Code - How it Works
(Page 2 of 4 )
So, how does this work? Let us start with basic facts. You will need one VB project. You will need a separate class for each page. That sounds like a lot of work especially if you have a large site. However, these classes are fairly small with no properties, one method and only several functions. Moreover, the classes are similar to each other, so there is going to be a lot of cut & paste. You will also need one ASP file. As this is where the users first touch your web site, it is here where we start to code. Create a file called exec.asp and add lines below to it.
<%@ Language=VBScript %> <% dim oInstance, sClass, sProject
set oInstance = Server.CreateObject(sProject & "." & sClass) oInstance.ProcessRequest set oInstance = nothing %>
From here on out, that is all ASP you will ever have to write. Say goodbye to it, spit on it, cuss at it and generally get all the anger out of your system before proceeding.
Let's analyse what this ASP script is doing. It expects two items in the QueryString collection: cls and prj. Then it puts them together to form a class string and attempts to create an object. So for instance, if you type in on the browser line:
...the ASP file assumes that you have a COM DLL with a class string called MyProject.View.
Futher down, notice the line oInstance.ProcessRequest. The ASP file assumes that the created object has a Public method called ProcessRequest. So obviously all the processing will go on in ProcessRequest method.
So far, so good - we need to create a class for each page and there should be a standard Public method called ProcessRequest...easy enough.
You maybe wondering at this point - how exactly am I going to get access to the ASP intrinsic objects? Well, because our COM object is being created with Server.CreateObject instead of CreateObject, the intrinsic objects will be available (with a little bit of code) in their full glory.
Now create a new ActiveX DLL project. Call the project "MyProject". Call the class "View".
Add the following to the class (you will have to add this to every single class that will be created the ASP script).
Option Explicit Private mobjContext As ASPTypeLibrary.ScriptingContext Private Const conHTMLTokenPrefix = "{|" Private Const conHTMLTokenSuffix = "|}" Public Sub OnStartPage(objContext As ASPTypeLibrary.ScriptingContext) Set mobjContext = objContext End Sub Public Sub OnEndPage() Set mobjContext = Nothing End Sub Public Sub ProcessRequest() On Error GoTo errProcessRequest
'Your code goes here '...
exitPoint: 'generic exit point Exit Sub errProcessRequest: 'write out the error to the browser Call mobjContext.Response.Write(Err.Description) GoTo exitPoint End Sub
Let's analyze this piece of code. The way ASP's Server.CreateObject works is that it looks for OnStartPage and OnEndPage methods to pass in the ASP context object (intrinsic objects, in other words). Once ASP passes in the object context intrinsic objects (Request, Response, Server, Application, Session) are available under the mobjContext super object. For instance, to write out a line of code you would use the following in the ProcessRequest sub.
mobjContext.Response.Write "My Test Line"
Pretty cool, huh? Keep in mind, you can place breakpoints in your ActiveX DLL and use the full power of the Visual Basic debugger.
Now you are probably thinking, the great thing about ASP was that I could create HTML page in my favorite editor and then intersperse VBScript code into it. Do I have to write HTML out one line at a time using this approach? Absolutely not - it would defeat the whole purpose of Rapid Application Development. In fact, our approach is relatively easy to implement.
We can achieve this ease by use of HTML templates. Let me explain. Let's say we have a page that has to be populated by the list of employees. So our final HTML page would look like this:
Obviously, we can't just send out a hard-wired list, as our employee list may change. So we then create a template. We replace the hardwired names with a tag, our engine will understand.
So in our code, we will get the list of employees from the database, read in this template, replace the EMPLOYEE LIST tag with our real employee list and send the text out using mobjContext.Response.Write method. Easy enough? But wait, there is more. You can wrap the whole process of reading templates and replacing tags into a reusable class (one is provided with the download).
So here is how the final draft of the ProcessRequest method will look like:
Public Sub ProcessRequest() Dim objTemplate As clsTemplateManager Dim strReturn As String
'****************************************** 'normally this code should be encapsulated 'in another object or module Dim oConn As New ADODB.Connection Dim oRs As New ADODB.RecordSet Dim sList As String
'open a connection to an Access database oConn.Open Provider="Microsoft.Jet.OLEDB.3.51;;Data Source=" & _ App.Path & "\Sample.mdb" 'populate the recordset with the names oRs.Open "select * from tblNames", oConn
'build an HTML list Do While Not oRs.EOF sList = sList & "<option value=" & oRs!ID & ">" & _ oRs!Name & "</option>"
oRs.MoveNext Loop Set oRs = Nothing Set oConn = Nothing '******************************************
'Template Manager Set objTemplate = New clsTemplateManager With objTemplate 'Add values and Tokens .TokenAdd sList, "EMPLOYEE LIST"
'Set Template Name and Tokens .TemplatePath = App.Path & "\Templates" & "\SeeName.htm" .TokenPrefix = gconHTMLTokenPrefix .TokenSuffix = gconHTMLTokenSuffix
'Combine tokens and template and return HTML page strReturn = .WriteResponse End With
' Write out the page Call mobjContext.Response.Write(strReturn)
exitPoint: 'generic exit point Exit Sub errProcessRequest: 'write out the error to the browser Call mobjContext.Response.Write(Err.Description) GoTo exitPoint End Sub
Here is a quick explanation on how to get this setup working on your computer.
Make sure you have IIS or PWS installed and working.
Then under c:\inetpub\wwwroot folder create a new directory called Sample. Unzip the downloaded project into this directory. Double-click on the MyProject.vbp file and click Run.
Open your favorite browser and go to http://localhost/Sample
That's all there is to it. Now you can place breakpoints in your VB code, learn how the code works and debug at will. The templates are location in the \Sample\Templates folder.
What does the downloaded program do? Included with the download there is a simple access database With a single table. This table contains a list of employees. This application lets you manipulate the names of employees.