Next, we will write a very simple component. The focus is on knowing how to develop DLL components, rather than its complicated code! These will all depend on your own efforts in the future. Write a small component (Introduction to 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 ASP, but also protect their own code. Writing this article is also In order to give an introductory lesson to netizens who want to develop components!
Next, we will write a very simple component. The focus is on knowing how to develop DLL components, rather than its complicated code! These will all depend on your own efforts in the future.
Server-side components
First of all, server-side components must be different from client-side components. Client-side components are transmitted through the network and rely on HTML to function. And they can only be used on IE. But server-side components run on the server side and are executed on the server. Various operations. 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 (it can also be the code between). If the ASP program has been called before, then it will Use the compiled program in memory to return HTML code to the user. If not, then it is recompiled. Here ASP has a little speed advantage over CGI, because CGI uses a thread for each request. This consumes a lot of time. Server resources.
Have you ever thought that the program you wrote can run on IIS!?! You can do it now! Using VB5 (now VB6 of course), you can create Dynamic Linked Libraries (DLL files), 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 a windows95+PWS+VB5 environment.
let's get started
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 Microsoft Active Server Pages Object Library, which is very useful in our program. Select Project from the menu, then select References, 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 own hobbies! Naming them is also very important. We will use this project name and class name to create instances of this component in the future! Details will be introduced later.
I don’t want to say more about how to change your name!
Our project name was changed to Exmaple, and the class name was Helloword
How to use projects and classes
Now we have our own project (Example1) and class name (HelloWorld). We will use their names in the ASP code to refer to this component in the future. In ASP we refer to it like this, as follows:
Set ObjReference = Server.CreateObject(ProjectName.ClassName)
The reference to our project is:
Set ObjReference = Server.CreateObject(Example1.HelloWorld)
Now we can use ObjReference to call the functions and subroutines we created in the component. Next we will write a SayHello subroutine. Our code to execute it is as follows:
In order to use ASP methods in the 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, whenever a user accesses an ASP file with this component, IIS will pass the scriptingContext to our object for us to use. This scriptingContext includes all ASP methods and properties. Implementationally, this gives us the ability to access All ASP objects. Look at the following code:
Copy the code code as follows:
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 to replace Application in ASP. In the same way, we can replace Request, Server..., but we have to declare these variables before OnStartPage:
Copy the code code as follows:
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 submitted forms. Now we implement this function in our VB, the code is as follows:
Implemented in ASP:
Implemented in VB:
Copy the code code as follows:
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 whatever you want, you can even just use Response.
Another thing we have to pay attention to is that we have to write the OnEndPage sub-function in the class we created. This OnStartPage is the opposite! OnStartPage creates objects, and OnEndPage destroys objects.
Copy the code code as follows:
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 sub-function to display Holle World. This SayHello method is just a sub-function in the HelloWorld class. We will use the following method to display this method in ASP in the future.
SayHello program is very simple!
Copy the code code as follows:
Public Sub SayHello()
MyResponse.Write(Hello World)
End Sub
Now that a small component has been written, the remaining work is to compile the component and save it in the project menu. You can give it any name. Let's use Exmaple1.vbp! Then select make exmaple1.dll in the menu and add It is compiled into a DLL file. A component is truly completed!
Note that after compiling this component, you must first turn off your PWS and then compile this component. Otherwise VB will tell you that some components are in use.
Use our own components in ASP.
When you have corrected the errors in the compilation and successfully compiled the example1 project, now you have to take out your favorite HTML editor to write the following statement and save it as an ASP file.
You can see the results after running:
Hello World
Register component
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. The following is an example of registration:
Regsvr32.exe C:/wwwroot/Example1/Example1.dll
In your system, VB will automatically register it for you, so you rarely use Regsvr32.exe
We just wrote a very small component here. You can write your own larger components, and you can also use many controls in VB.
Let's use components to expand the functionality of our program! I also hope to see more components from us Chinese people.