How to write ASP into Dll_ application skills

Source: Internet
Author: User
This article is mainly the ASP code into components, the developer is not only to speed up the ASP, but also to protect their own code.
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 the References window appears
Select the Microsoft Active Server Pages Object Library from.

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 changed to 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 in this class
Child functions, 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 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 ASP's Objects
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, Myresponse is a name that can be easily fetched and you can even take response.
Another thing we should be aware of is that we have to write the OnEndPage child function in our established class, and 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



SayHello method
Let's create a child function to display "Holle world". This SayHello method is just helloworld a child function in this class, we will 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 whatever name can be, we use EXMAPLE1.VBP Bar! Then use the menu to select "Make Exmaple1.dll" and 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")
Objreference.sayhello
%>

〈/body>
〈/html>

You can see the results after running:

Hello World

Registering components

If you want to share your components with your friends and neighbors, then you have to register your components on your system. We typically use Regsvr32.exe to register components. Your component will appear in Win95/win98 after registration windows/ System directory. Here is an example of a registration:

Regsvr32.exe C:/example1/example1.dll

In your system, VB will automatically register you, so you rarely use the Regsvr32.exe

We just write a very small component here, you can write your own larger components, but also can use a lot of VB control.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.