Visual This article is mainly for people who want to improve their ASP level to write! The ASP code into a component, the developer is not only to speed up the ASP, but also to protect their own code. This article is written, but also to give users want to develop components of an introductory lesson! Next, we'll write a very simple component, focusing on knowing how to develop DLL components, not their complex code! It all depends on your own efforts in the future.
Server-side components
First, server-side components are different from the client's components. The client's components are transmitted over the network, depending on the HTML. And can only be useful on IE. but the server-side component is run on the server side, and it performs various operations on the server. So, all browsers are available, It relies on the server rather than the browser.
When IIS is requested to execute an ASP program, it first finds the code between the 〈%%> tags in the ASP file, and executes it (or it can be code between 〈script runat=server>〈/script>). If the ASP program was previously invoked, it would use an in-memory compiled program to return the HTML code to the user, and if not, it would recompile. Here ASP has a little more speed advantage than CGI, Because CGI is a thread used for every request. This greatly consumes the resources of the server.
If you don't want to write the program yourself, you can run it in IIS!?! You can do it now! With VB5 (now VB6, of course), you'll be able to create dynamic linked libraries (DLL files) that can run directly on IIS (if an ASP file is requested).
Requirements for systems and software
You need a 32-bit operating system to run the ASP. Of course you also have to install IIS or PWS. Our following program is developed under the WINDOWS95+PWS+VB5 environment.
Here we go
Start your VB, 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'll get rid of all two names. Before renaming, please confirm that we have Microsoft Active Server Pages Object Library, It's very useful in our program. Select project from the menu, and then select References in which you will see the References window select Microsoft Active Server Pages Object Library.
Naming Projects and classes
Now we come to the name of Project1 and Class1 according to their hobbies! It is also important to name them, and we will use this project name and class name to create an instance of this component!
How to change my name, I don't want to say more! Our project name is Exmaple, class name is Helloword
How to use Engineering and class
Now we have our own engineering (EXAMPLE1) and class name (HelloWorld). We'll use their names in the ASP code to refer to this component in the future. In the ASP we are quoting 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 invoke the function we created in the component, subroutine. Below we will write a SayHello subroutine, we execute it with the following code:
〈%
Set objreference = Server.CreateObject ("Example1.helloworld")
Objreference.sayhello
%>
In order to use the ASP method in the Helloword class, you must write a onstartpage child function in this class.
Public Sub OnStartPage (Passedscriptingcontext as ScriptingContext)
Set Myscriptingcontext = Passedscriptingcontext
End Sub
Now, whenever a user accesses an ASP file with this component, IIS sends ScriptingContext to our object for us to use. This scriptingcontext includes all ASP methods and properties. Implementation, This gives us the ability to access all of the ASP's objects. Look at 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
We can use MyApplication in VB to replace the application in ASP, the same can replace request,server ... but we are here 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
Using an ASP object 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 are able to use all response methods, and of course, the Myresponse name can be easily fetched, and you can even take response. Another thing we have to be aware of is that we have to be in our established class, Write the OnEndPage child function, this onstartpage is the opposite! OnStartPage is the object of creation, OnEndPage is the object of extinction.
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 lets you create a child function to display "Holle world". This SayHello method is only HelloWorld a child function in this class, we will later use the following display in ASP
〈%
Set objreference = Server.CreateObject ("Example1.helloworld")
Objreference.sayhello
%>
SayHello's program, very simple!
Public Sub SayHello ()
Myresponse.write ("Hello World")
End Sub
Now a small component to write, the rest of the work is to compile this component, in the "Project" menu to save it, take any name can be, we use EXMAPLE1.VBP Bar! Then use the menu to select Make Exmaple1.dll, Compile it into a DLL file. A component is really done!
Note that this component is compiled so you have to turn off your PWS before compiling this component. Otherwise VB will tell you some components in use.
Use our own components in the ASP.
When you have corrected the error in the compilation, successfully compiled the EXAMPLE1 project, now you have to take out your favorite HTML editor to write down the following statement, save as an ASP file.
〈html>〈head>〈title>example 1〈/title>〈/head>
〈body>
〈%
Set objreference = Server.CreateObject ("Example1.helloworld")