Write a widget (beginner of components)
This article is mainly written for people who want to improve their ASP level! By turning ASP code into components, developers not only speed up the ASP speed, but also protect their own code. This article is also written by this article. In order to teach an introductory course to netizens who want to develop components!
Next, we will write a very simple component, the focus is to know how to develop DLL components instead of its complex code! These depend on your own future efforts.
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 are run on the server side and it executes on the server. Various operations. Therefore, all browsers can enjoy it, relying 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 (can also be the code between the two). If the ASP program has been called before, it will Use compiled programs in memory to return HTML code to the user. If not, then it will be recompiled. Here ASP has a little more speed advantage over CGI, because CGI uses a thread for every request. This consumes a lot The resources of the server.
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 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 make sure that we have the Microsoft Active Server Pages Object Library, which is very useful in our programs. Select Project from the menu and then select References there, and a reference window will appear. From there, 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:
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 and ask us to use it. 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:
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 display method in ASP in the future
SayHello's program is very simple!
Public Sub SayHello()
MyResponse.Write (Hello World)
End Sub
Now that a small component is written, the rest of the work 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 then select make example1.dll. It is compiled into a DLL file. One 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.
After running, you can see the results:
Hello World
Register components
If you want to share your components with your friends and neighbors, then you have to register your components on your system. We generally use Regsvr32.exe to register components. After registration, your components will appear in Win95/Win98. In the windows/system directory. Here is an example of registration:
Regsvr32.exe C:/wwwroot/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.
Let’s use components to expand the functions of our program! We also hope to see more and more components of our Chinese.