This article mainly turns the ASP code into components, but also the speed of accelerating the ASP, but also protects your code.
Below, we will write a very simple component, focusing on how to develop DLL components, not their complex code! These must rely on yourself.
Server-side components
First, the server-side components must be different from the client's components. The client's components are transmitted over the network, relying on HTML to work. But only use it on IE. But the server side is running on the server side, which is executed on the server Various operations. Therefore, all browsers can be enjoyed, depending on the server instead of the browser.
When IIS is requested to execute an ASP program, it will first find the code between the <%%> tag in the ASP file, and execute it (or code between
I don't want you to write the program you can run in IIS !! Now you will do it! Use VB5 (of course now VB6), you can build DynamiClinkedLibraries (DLL file), it can run directly on IIS (if there The ASP file is requested.
System and software requirements
You need a 32-bit operating system to run the ASP. Of course, you have to install IIS or PWS. We will develop under Windows95 PWS VB5.
let us 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). We will change the two names. Before the rename First confirm that we have MicrosoftActiveServerPageSObjectLibrary, which is very useful in our programs. Select "Project" from the menu, then select "reference", will appear "Reference" windows
Select MicrosoftActiveServerPageSObjectLibrary from it.
Name your project and class
Now let's name it based on your hobbies! It is also important to name them. We will use this engineering name and class name to create instances of this component!
How to change your name, I don't want to say more!
Our engineering name is changed to Exmaple, named HelloWord
How to use engineering and classes
Now we have our own project (Example1) and class name (HelloWorld). We will use their names in the ASP code to reference this component. We reference this in ASP, as follows:
SetObjreference = Server.createObject ("Projectname.className)
Quote for our project is:
SetObjreference = Server.createObject ("eXample1.helloworld")
Now we can use Objreference to call our functions created in the component, subroutine. Below we will write a SayHello subroutine, we execute its code as follows:
<%
SetObjreference = Server.createObject ("eXample1.helloworld")
Objreference.SAYHELLO
%>
In order to use ASP methods in the HelloWord class, you must write an onStartPage in this class.
Sub function. As follows:
PublicSubonstartPage (PassedscriptingContextAsscriptingContext)
SetMyscriptingContext = PassedScriptingContext
EndSub
Now, whether the user visits an ASP file with this component, IIS will transfer ScriptingContext to our objects. This scriptingContext includes all ASP methods and properties. Implementation, this makes we have an ability to access Objects of all ASPs. Look at the following: PUBLICSBONSTARTPAGE (PassedscriptingContextAsscriptingContext)
SetMyscriptingContext = PassedScriptingContext
SetMyapplication = myscriptingContext.Application
SetMyRequest = myscriptingContext.Request
SetMyResponse = myscriptingContext.Response
SetMyServer = myscriptingContext.server
SetMySession = myscriptingContext.Session
EndSub
In the future, we can use MyApplication in VB to replace the Application in the ASP, which can replace Request, Server ..., but we come to declare these variables before onStartPage:
PrivateMyscriptingContextAsscriptingContext
PrivateMyApplicationAsApplication
PrivateMyRequestasRequest
PrivateMyResponseasResponse
PrivateMyServeras Server
PrivateMysessionAsSession
Objects using ASP
Our variables can now be used like a standard ASP object! For example, we often use request.form () in the ASP to collect data submitting forms. Now we implemented this function in our VB, the code is as follows:
Implement with ASP:
<%
Mytempvariable = Request.form ("UserName")
Response.write ("Yountered" & mytempvariable & "readyourusername")
%>
Implementation in VB:
Mytempvariable = myRequest.form ("UserName")
MyResponse.write ("Yountered" & mytempvariable & "askOOOOUSERNAME")
By using MyResponse instead of Response, we can use all response methods, of course, MyResponse name can be taken casually, you can even take Response.
Another thing we have to pay attention to, we have to write on our created class, this OnStartPage is the opposite! OnStartPage is an object, OneendPage is destroyed.
PublicsubonendPage ()
SetMyscriptingContext = Nothing
SetMyApplication = Nothing
SETMYREQUEST = Nothing
SetMyResponse = Nothing
SetMyServer = Nothing
SetMySession = Nothing
EndSub
Sayhello method
Let's create a sub-function to display "HolleWorld". This SayHello method is just a subfunction in the class of HelloWorld, and we will use the following display this method in the ASP.
<%
SetObjreference = Server.createObject ("eXample1.helloworld")
Objreference.SAYHELLO%>
Sayhello's program is very simple!
Publicsubsayhello ()
MyResponse.write ("HelloWorld")
EndSub
Now a small component is written, the rest is to compile this component, save it in the "Project" menu, what is the name, we use exmaple1.vbp! Then use "MakeExMaple1.dll in the menu" ", Compile it into a DLL file. A component is really completed!
Note that you have to turn off your PWS first, then compile this component. Otherwise, VB will tell you some components in use.
Use our own components in the ASP.
When you corrected the error in compilation, successfully compile the Example1 project, now you have to take out your favorite HTML editor to write down the following statement, save as an ASP file.
HEAD>
<%
SetObjreference = Server.createObject ("eXample1.helloworld")
Objreference.SAYHELLO
%>
Body>
Html>
You can see the result after running:
HelloWorld
Registration component
If you want to share your components with your friends, 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. Below is an example of registration:
Regsvr32.exec: /wwwroot/example1/example1.dll
In your system, VB will automatically register you, so you rarely use regsvr32.exe
Here is just a very small component, you can write your bigger components, and you can use a lot of controls in VB.