Handwritten widgets (Component entry) asp

Source: Internet
Author: User

Write a widget (Component entry)

This article is intended for those who want to improve their ASP level! Turning ASP code into a component not only speeds up ASP, but also protects your own code. This article is written to give an introductory lesson to users who want to develop components!

Next, we will compile a very simple component, focusing on how to develop the DLL component, rather than its complex code! All of this depends on your future efforts.

Server Components

First, the server components should be different from the client components. the client components are transmitted over the network and work with HTML. and can only be used on IE. however, the server component runs on the server, and it performs various operations on the server. therefore, all browsers can enjoy it, relying on servers rather than browsers.

When IIS is requested to execute an ASP program, it first finds the code between tags in the ASP file and executes it (or code between tags ). if this ASP program was previously called, it will return HTML code to the user using the compiled program in the memory. If not, it will re-compile. here, ASP is a little more speed advantage than CGI, Because CGI uses a thread for every request. in this way, the server resources are greatly consumed.

I don't want the program you write to run on IIS !?! Now you can do it! When using VB5 (of course, it is VB6 now), you can create a Dynamic Linked Libraries (DLL file), which can be run directly on IIS (if there is an asp file for request ).

System and software requirements

You need a 32-bit operating system to run ASP. Of course, you also need to install IIS or PWS. Our program is developed in the Windows 95 + 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 provides a default project name (project1) and Class Name (class1 ). we will change both names. before renaming, make sure that we have the Microsoft Active Server Pages Object Library, which is useful in our programs. select "project" from the menu, and then select "Reference". The "Reference" window appears, from which you can select Microsoft Active Server Pages Object Library.

Name projects and Classes

Now let's name project1 and class1 based on our hobbies! Naming them is also very important. We will use this project name and class name to create an instance for this component in the future! The following is a detailed description.

How can I change my name!
Change the project name to Exmaple, and the class name is Helloword.

How to use projects and Classes

Now we have our own project (Example1) and Class Name (HelloWorld ). in the future, we will use their names in ASP code to reference this component. in ASP, We reference it as follows:

Set ObjReference = Server. CreateObject ("ProjectName. ClassName ")

References to our project are:

Set ObjReference = Server. CreateObject ("Example1.HelloWorld ")

Now we can use ObjReference to call the function we created in the component. We will write a SayHello subroutine. The code we run is as follows:

To use ASP in the Helloword class, you must write an OnStartPage
The sub-function is as follows:

Public Sub OnStartPage (PassedscriptingContext As scriptingContext)
Set MyscriptingContext = PassedscriptingContext
End Sub

Now, no matter when the user accesses an ASP file with this component, IIS will send scriptingContext to our object for use. this scriptingContext includes all ASP methods and attributes. this allows us to access all ASP objects. see the following code:

Copy codeThe Code is 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 the Application in ASP. Likewise, we can replace the Request, Server..., but we want to declare these variables before OnStartPage:

Copy codeThe Code is 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

Use ASP objects

Our variables can now be used like standard ASP objects! For example, we often use Request. form () in ASP to collect the data for submitting forms. Now we implement this function in our VB. The Code is as follows:

Implemented in ASP:

Implemented in VB:Copy codeThe Code is 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 of MyResponse can be obtained at will, and you can even get Response.
Another thing we should note is that we have to write the OnEndPage subfunction in the class we created. This OnStartPage is the opposite! OnStartPage is the object to be created, and OnEndPage is the object to be destroyed.

Copy codeThe Code is 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 only a sub-function in the HelloWorld class. We will use the following method in ASP later.

The SayHello program is very simple!

Copy codeThe Code is as follows: Public Sub SayHello ()
MyResponse. Write ("Hello World ")
End Sub

Now the compilation of a small component is complete, and the rest of the work is to compile this component. Save it in the "project" menu and get any name. Let's use Exmaple1.vbp! Then select "make exmaple1.dll" in the menu and compile it into a DLL file. A component is actually complete!

Note: after this component is compiled, 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 compilation error and successfully compiled the example1 project, now you have to come up with your favorite HTML editor to write down the following statement and save it as an ASP file.

The result is displayed after running:

Hello World

Register Components

Register to register the component. After registration, your component will appear in the windows/system directory of Win95/Win98. The following is an example of registration:

Regsvr32.exe C:/wwwroot/Example1/Example1.dll

In your system, vbwill automatically give you registration, so you rarely use regsvr32.exe

We only wrote a very small component here. You can write your own larger component, and you can also use many widgets in VB.
Let's use components to expand the functions of our program! We also hope to see a lot of Chinese components.

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.