This article is mainly written for people who want to improve their ASP level! Turn the ASP code into components. Developers not only speed up the speed of ASP, but also protect their code. This article is written. In order to get a entry class for netizens who want to develop components! Below, we will write a very simple component, the point is to know how to develop DLL components, not its complicated code! These will rely on your own efforts in the future.
Server component
First of all, the component of the server should be different from the client's component. The client component is transmitted through the network and rely on HTML to work. It can only be useful on IE. Various operations. Therefore, all browsers can be enjoyed, it depends 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 (also the code between" Script Runat = Server> </Script>). If this ASP program has been called before, it will use the compiled program in the memory to return the HTML code to the user. If not, it will be re -compiled. Here ASP is a little speed advantage than CGI because CGI It is every request using a thread. This greatly consumes the server's resources.
Do you want to run the program you write yourself!?! Now you can do it! Use VB5 (of course now VB6), you can build Dynamic Linked Libraries (DLL file), which can run directly on IIS ( If there are ASP files to request),).
Systems and software requirements
You need a 32 -bit operating system to run ASP. Of course, you also have to install IIS or PWS. Our program below is developed in the environment of Windows95+PWS+VB5.
Let's start
Start your VB, select the ActiveX icon. This icon can be found in the new project! VB will provide a default engineering name (Project1) and class name (class1). Please first confirm that we have Microsoft Active Server Pages Object Library. It is very useful in our program. Select the project from the menu, and then select a reference in it. Select the Microsoft Active Server Pages Object Library.
Name for engineering and class
Now let's name it Project1 and Class1 according to our hobbies! It is also important to name them. We will use this project name and class name to create an example of this component in the future! After detail, we will introduce it in detail later.
How to change the name, I don't want to say more! Our engineering name is changed to exmaple, and the class name is hello our
How to use engineering and classes
Now we have our own project (Example1) and the category name. In the future, we will use their names in the ASP code to quote this component. In ASP, we will quote in this way, as follows: as follows:
set object = 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 we created in the component, subroutine. Let's write a subroutine of Sayhello. We execute its code as follows:
< /%
set object = server.createObject (example1.Helloworld)
objreference.sayhello
%>
In order to use the ASP method in the HelloWord class, you must write an OnStartPage sub -function in this category. As follows:
PUBLIC SUB OnStartPage
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 objects, please use it. All ASP objects. Look at the following code:
PUBLIC SUB OnStartPage
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 the Application in ASP. The same is true that you can replace the request, server ....., but we come to declare these variables before the 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
使用asp的对象我们的变量现在就能像标准的asp对象来使用了!比如,我们经常在asp中用request.form()来收集提交表单的数据.现在我们在我们的vb中实现这个功能, The code is as follows:
Implement with ASP:
< /%
MyTempvariable = request.form (username)
Response.write (you Entected & MyTempvariable & As Your User Name)
%>
Implement in VB:
MyTempvariable = Myrequest.Form (username)
MyResponse.write (You Entered & MyTempvariable & As Your User Name)
By using myResponse to replace Response, we can use all Response methods. Of course, the name MyResponse can be taken casually, and you can even take Response. Another thing is that we have to pay attention to the class we have established. , Write the OnendPage sub -function, this OnStartPage is the opposite! OnStartPage is the object of creating, onendpage is the object of eliminating the destroyer.
Public Sub OnendPage ()
set myscriptingContext = Nothing
set myApplication = Nothing
set myrequest = nothing
set myresponse = nothing
set myserver = nothing
set mysession = nothing
end sub
The Sayhello method We will create a sub -function to display the Holle World. This Sayhello method is just a sub -function in the HelloWorld.
< /%
set object = 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 compiled, the rest is to compile this component, save it in the engineering menu, you can take any name, let's use exmaple1.vbp! Then use Make Exmaple1.dll in the menu, and it will be. It is compiled to DLL file. A component is really completed!
Note, compile this component, then you have to turn off your PWS first, and then compile this component. Otherwise, VB will tell you some components in use.
Use our own components in ASP.
When you correct the error in compilation and successfully compile the project Example1, now you have to take out your favorite HTML editor to write the following statements and save it as an ASP file.
"HTML> <Head> <Title> Example 1 </Title> </Head>
<Body>
< /%
set object = server.createObject (example1.Helloworld)
|||objreference.sayhello
%>
</Body> </html>
After running, you can see the result:
hello worldd
Registered component
If you want to share your components with your friends and neighbors, then you have to register your component on your system. We generally use regsvr32.exe In Windows/System directory. The following 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 write a very small component here. You can write your larger components, and you can also use many controls in VB.
Let's use components to expand the function of our program! I also hope to see a lot of components of our Chinese people.