Turning ASP code into components is not only to speed up the ASP, but also to better protect your own code. Let’s write a tutorial on writing ASP into DLLs. Let’s follow the editor of Error New.
Server-side components
First of all, the components on the server side should be different from those on the client side. The components on the client side are transmitted over the network and rely on HTML to work. They can only be useful in IE. However, the components on the server side run on the server and perform various operations on the server. Therefore, all browsers can enjoy it, and it relies on the server rather than the browser.
When IIS is requested to execute an ASP program, it will first find the code between the <% %> tags in the ASP file and execute it (or the code between <script runat=server> </script>). If this ASP program has been called before, it will use the compiled program in memory to return HTML code to the user. If not, it will recompile. Here, ASP has a little more speed advantage than CGI, because CGI uses a thread for each request. This greatly consumes the server's resources.
Do you want the program you wrote to run in IIS by yourself!?! Now you can do it! Using VB5 (of course it is VB6 now), you can create Dynamic Linked Libraries (DLL file), which can be run directly on IIS (if there is an asp file to request).
System and software requirements
You need a 32-bit operating system to run ASP. Of course, you also have to install IIS or PWS. Our following program was developed in the Windows95+PWS+VB5 environment.
Let's start
Start your VB and select the ActiveX icon. This icon can be found in the new project! VB will provide a default project name (project1) and class name (class1). We will change both names. Before changing the name, please first confirm that we have the Microsoft Active Server Pages Object Library, which is very useful in our programs. Select "Project" from the menu and select "Reference" in it, and the "Reference" window will appear.
Select Microsoft Active Server Pages Object Library.
Name projects and classes
Now let’s name project1 and class1 according to our hobbies! Naming them is also very important. We will use this project name and class name to create an instance of this component in the future! This will be introduced in detail later.
I don’t want to say more about how to change the name!
Our project name is changed to Example and the class name is Helloword
How to use engineering and classes
Now we have our own project (Example1) and class name (HelloWorld). In the future, we will use their names to reference this component in the ASP code. In ASP we will reference it like this, as follows:
Set ObjReference = Server.CreateObject("ProjectName.ClassName")
The quote for our project is:
Set ObjReference = Server.CreateObject("Example1.HelloWorld")
Now we can use ObjReference to call the function and subroutine we created in the component. Next, we will write a SayHello subroutine, and our code to execute it is as follows:
(%
Set ObjReference = Server.CreateObject("Example1.HelloWorld")
ObjReference.SayHello
%>
In order to use ASP methods in Helloword class, you must write an OnStartPage in this class
Subfunction. As follows:
Public Sub OnStartPage(PassedScriptingContext As ScriptingContext)
Set MyScriptingContext = PassedScriptingContext
End Sub
Now, no matter when the user accesses an ASP file with this component, IIS will send the ScriptingContext to our object. This ScriptingContext includes all ASP methods and properties. In terms of implementation, this allows us to access all ASP objects. See the following code:
Public Sub OnStartPage(PassedScriptingContext As ScriptingContext)
Set MyScriptingContext = PassedScriptingContext
Set MyApplication = MyScriptingContext.Application
Set MyRequest = MyScriptingContext.Request
Set MyResponse = MyScriptingContext.Response
Set MyServer = MyScriptingContext.Server
Set MySession = MyScriptingContext.Session
End Sub
In the future, we can use MyApplication in VB instead of Application in ASP, and in the same way, it can replace Request, Server..., but we want to declare these variables before OnStartPage:
Private MyScriptingContext As ScriptingContext
Private MyApplication As Application
Private MyRequest As Request
Private MyResponse As Response
Private MyServer As Server
Private MySession As Session
Objects using ASP
Our variables can now be used like standard ASP objects! For example, we often use Request.form() in ASP to collect data for submitting forms. Now we implement this function in our VB, the code is as follows:
Implemented in ASP:
(%
MyTempVariable = Request.Form("userName")
Response.Write ("you entered "& MyTempVariable & "as your user name")
%>
Implemented in VB:
MyTempVariable = MyRequest.Form("userName")
MyResponse.Write ("you entered "& MyTempVariable & "as your user name")
By using MyResponse instead of Response, we can use all Response methods. Of course, the name MyResponse can be used casually, and you can even choose Response.
Another thing we have to note is that we have to write the OnEndPage subfunction in the class we created, and this OnStartPage is the opposite! OnStartPage is to create an object, and OnEndPage is to destroy the object.
Public Sub OnEndPage()
Set MyScriptingContext = Nothing
Set MyApplication = Nothing
Set MyRequest = Nothing
Set MyResponse = Nothing
Set MyServer = Nothing
Set MySession = Nothing
End Sub
SayHello method
Let's create a subfunction to display "Holle World". This SayHello method is just a subfunction in the HelloWorld class. We will use the following method to display it in ASP in the future
(%
Set ObjReference = Server.CreateObject("Example1.HelloWorld")
ObjReference.SayHello
%>
SayHello's program is very simple!
Public Sub SayHello()
MyResponse.Write ("Hello World")
End Sub
Now that a small component is written, the rest is to compile this component, save it in the "Project" menu, and you can choose any name. Let's use Example1.vbp! Then select "make example1.dll" in the menu and compile it into a DLL file. A component is truly completed!
Note that if you compile this component, you have to turn off your PWS first and then compile this component. Otherwise, VB will tell you that some components are in use.
Use our own components in ASP.
When you correct the error in compilation and successfully compile the example1 project, you have to take out your favorite HTML editor and write down the following statement and save it as an ASP file.
〈HTML>
〈HEAD>
〈TITLE>Example 1〈/TITLE>
〈/HEAD>
〈BODY>
(%
Set ObjReference = Server.CreateObject("Example1.HelloWorld")
ObjReference.SayHello
%>
〈/BODY>
〈/HTML>
After running, you can see the results:
Hello World
Register components
If you want to share your components with your friends and neighbors, you have to register your components on your system. We generally use Regsvr32.exe to register components. After registration, your components will appear in the Windows/system directory of Win95/Win98. Here is an example of registration:
Regsvr32.exe C:/Example1/Example1.dll
In your system, VB will automatically register for you, so you rarely use Regsvr32.exe
We just wrote a very small component here. You can write your own larger components and use many controls in VB.
The key to writing DLLs by ASP is to know how to develop DLL components. Developing DLL components is not complicated code, and it requires learning through your own efforts.