Using VB to create ASP server-side components

Source: Internet
Author: User
Tags definition iis integer integer numbers reference require
Create | Server use VB to create ASP server-side components
 
This article, by comparing with the traditional design method, describes how to invoke the VB component in ASP code. In this article, we assume that the reader has a knowledge of VB and ASP related to getting started.

Server-side component and client component comparisons

There are many differences between server-side components and client components. Server-side components are DLL files that are registered on the computer server, and the client components are registered on the computer where the browser is running, and in IE, these client components are called ActiveX Browser plug-in components.

The ActiveX client component can be written using VB and sent to the browser via the Internet or intranet to produce wonderful results. The problem is that ActiveX client components are limited to IE, and server-side components written using VB can produce pure HTML code that works for all browsers. The biggest problem with server-side components is that the component must be run in an windows+iis environment or in an application compatible with the IIS API. In comparison, it seems easier to implement this compatibility on the server side.

The IIS server-side components reside in the same memory space as IIS and are ready to be invoked on the ASP Web pages processed on the server. Theoretically, we can insert any text or code into the ASP code that returns the browser, but in general, most server-side components are used to process computationally or database information lookups that require a lot of time, and then return the resulting results to the browser in the context of the HTML code.

Analysis of VB Component

Since this article is intended to discuss the basic methods of writing VB components, the examples in this case will be very simple. Before we discuss the writing of VB components in detail, we will first analyze the VB components from the concept.

When using VB to write server-side components, there are three layered concepts (used in both VB and ASP code) that need to be noted:

· Project name

· Class name

· Method name

The name of the VB project is Project name. Many developers view project names as component names, but VB sees it only as the name of the project. In our case, project name is Exampleproject, of course, we can name our own engineering names at will, class name is Exampleclass,method name is Examplemethod.

The project name (component name) can also be the name of the DLL file that is compiled by the component code, which will contain compiled VB code that is used by IIS to return text or HTML code to the browser.

The method name refers to the part of the VB code that manages the functionality of a particular code, such as calculating a date or displaying a list of all the authors in the database. The component method has a bit of a black box that completes a particular work or returns specific information based on the information entered. In general, you can have more than one method in a component. To manage the components more effectively, you can combine the methods with similar categories, which is the role of the component class.

A component class can generate a copy of the component class code in memory, which is also called an object when it is created using ASP code, which is instantiation. Once you have an object reference to the component class code instance, we can invoke the method contained in the class from the ASP code.

In our example, the name of the project, class, and method will be used to instantiate the VB component in the ASP code and transfer the value from the ASP code to the VB code in the form of the method parameter, and receive the value returned from the VB method in the ASP code.

Calling the VB component from an ASP file

The ASP file we use to invoke the VB component will hold the reference to the VB object using the object variable. In an ASP file, you can use the CreateObject () method of the ASP server object to create an object that returns a reference to the object it creates. In the example, we will use Objreference as the object variable for the component. The following code shows the ASP code needs to use the component's project name and class name (Exampleproject and ExampleClass) when instantiating the VB component.

Instantiate the ASP code for the VB component:

Set objreference = Server.CreateObject ("Exampleproject.exampleclass")


The VB component will accept the value of 3 variables from the ASP code and return a value to the ASP code, which will be stored in the ASP variable named Strmethodreturn. The following code shows how the ASP code to get the value returned by the VB component, it transmits three names to the VB method of Param1, PARAM2 and Parma3 three parameter values:

Strmethodreturn = Objreference.examplemethod (Param1, Param2, PARAM3)

PARAM1, PARAM2, Param3 These three parameters must be exactly the same as the definition of the method in the VB component, the following is an example of a two-line instantiation of the VB component's class and the method of invoking the class to obtain the ASP code for the return value:


Set objreference = Server.CreateObject ("Exampleproject.exampleclass")
Strmethodreturn = Objreference.examplemethod (Param1, Param2, PARAM3)



The chart below visually shows how the VB component's engineering, class, and method names are coordinated with the component instantiation code in the ASP file. When you are learning how to write VB code and ASP files in an example, you can use the following chart as a reference.





The function of VB method

The simple VB component in our example will get the user's name and age, and then return a day-old user's age, and have an option to remind a user whether he or she is over 45 years of age.

If we send a fictitious Eric Clapton to the component as the first parameter value of the method, the second parameter is set to 56, we will get the following return string:

Eric Clapton is over 20440.

If we set the optional third argument to True (this parameter will let the method determine whether the user is over 45 years old), we will get the following return string:

Eric Clapton is over 20440.

With three completely different variables ━━ the user's name, age, and whether they are over 45 years old, we need to use three method parameters to transfer this information from the ASP file to the VB code. In VB, it is important to consider which data types to use. We will use a string variable named strname to represent the user's name, and an integer variable named intage indicates the age of the user, and a Boolean variable named Blnageemphasison indicates whether the user is over 45 years old.

Three method parameters (variables transmitted to the method code of the VB component):

StrName (String)
IntAge (Integer)
Blnageemphasison (Boolean)


Creating server-side components in VB

After you start VB, double-click the ActiveX DLL icon in the New Project window. Once VB loads a new ActiveX DLL project, you will see at least two open windows: the Engineering window and the Properties window. If you have a window that doesn't appear, you can choose View menu items from the VB menu (see-> project manager, view-> Properties window, respectively).

Because VB to the first project and class of the default naming is Project1, Class1, we can change them to Exampleproject and ExampleClass respectively. Modification of the project name can be done in the Project window. The new project name entered in the engineering window has a small box with + or-on the left. If the + number is displayed, select the Small box, and the + number becomes the-number, and the default class name (CLASS1) is displayed under the project name. Select the default class name in the Project window and modify the default class name to ExampleClass in the Properties window.

When saving the project, VB saves the code containing the class in a file with an extension of CLS, with an extension of vbp, which stores the various settings for the project, the file name, and the location of the file store.

Attribute values for server-side components

Displays the properties of the ExampleClass class in the Properties window, noting that the value of the Instancing property is "5 multiuse" and that the value of the property will change if the type of the project is set to the Standard EXE project.

In the VB menu select "Engineering"-> "Exampleproject Properties", will display the Project Properties window. The value of the threading mode attribute at the lower right of the General tab should be set to cell thread, which will allow multiple visitors to use different instances of our component classes at the same time. In addition, select the unattended execution and memory resident two options to avoid memory leaks in the VB6.

Code of VB Method

Now we need to use the VB Code window to enter the VB code. If the code window is still blank, enter the following code:

Option Explicit
' It will require us to define all the variables.

Public Function Examplemethod (ByVal strName as String, _
ByVal IntAge as Integer, _
Optional ByVal Blnageemphasison as Boolean = False) as String




In the above code, we define the method as a public function, which means that any code outside the component can call it, and because it is a function, it returns a value to the code that calls it.

Public Function Examplemethod () as String


The above code indicates that the Examplemethod () function will return a value of the string type to its caller.

Our VB method has 3 parameter variables that accept values from the ASP code, and the last parameter variable is optional. All parameter variables used to receive values from outside the VB component need to be defined and used in parentheses between VB methods, and we can use variables defined as method parameters in this way as variables defined in the method, the only difference being that the outer ASP code determines their value.

Here are three variables and their data types:


ByVal StrName as String
ByVal IntAge as Integer
Optional ByVal Blnageemphasison as Boolean = False



The code above defines the data types of three method parameters, indicating that they are passed by value, and that the third argument is optional and, if there is no third argument, the default is False.

Then, we will add some necessary commas, spaces, and underscores (_) in the definition of the method in order to conform to the syntax requirements of VB. We'll place the parameter list in the parentheses between the method definitions, and the resulting method is defined as follows:

Public Function Examplemethod (ByVal strName as String, _
ByVal IntAge as Integer, _
Optional ByVal Blnageemphasison as Boolean = False) as String



When you enter the method definition above in the VB Code window, an End Function statement is generated. Between the definition of a method and the End function is where we write our own code.

The first line of code that we add to the body of a method is to define a string variable that stores the string data returned by the method. We can use strings to return text data to the code that invokes the method without using a string variable.

Dim strreturnstring as String
Here we can build the returned string. We can use the strname variable value that is passed by ASP code through the parameter list of the method. First, the strname parameter variable value is concatenated with the string "is over". Next we will use the Intage parameter variable to compute a live number of days, and then add the "age in" string to the previous string. It should be noted that we need to convert the product of Intage * 3,652 integer numbers to a string, and then combine it in the strreturnstring string, and the CStr () method in VB can achieve this.

strreturnstring = StrName & "are Over" & CSTR (IntAge * 365)


If you assume that the name of the component passed from ASP code is Eric Clapton, the age parameter is 56, so strreturnstring should include the following:

Eric Clapton is over 20440

Our final string will be added based on whether the value of the intage variable exceeds 45 and the Blnageemphasison variable is set to True

"Days old" or "days old". The following code can implement this feature:

If Blnageemphasison and IntAge > Then
strreturnstring = strreturnstring & "days old."
Else
strreturnstring = strreturnstring & "days old."
End If


If the ASP code does not pass the value of the Blnageemphasison variable to the component as a method parameter, its value is set to False by default, as defined by our method. If it is set to true and the value of the intage variable is greater than 45, we will get the following output:

Eric Clapton is over 20440.

Otherwise, we'll get the following output:

Eric Clapton is over 20440.

To return the above string to the calling component's ASP code, we assign the value of the string to the name of the method:

Examplemethod = strreturnstring


The complete method code looks like this:

Public Function Examplemethod (ByVal strName as String, _
ByVal IntAge as Integer, _
Optional ByVal Blnageemphasison as Boolean = False) as String

'/////set up local variables
Dim strreturnstring as String

'/////Create the value of the returned variable
strreturnstring = StrName & "are Over" & CSTR (IntAge * 365)

'/////Perfect strreturnstring
If Blnageemphasison and IntAge > Then
strreturnstring = strreturnstring & "days old."
Else
strreturnstring = strreturnstring & "days old."
End If

'/////return string
Examplemethod = strreturnstring

End Function


Calling the VB method in ASP code

Instantiate a VB object in ASP code

Most of the ASP code we need has been discussed in front of a conceptual overview. In the ASP code, we still need to complete the following work in sequence:

• Instantiate the VB component using the CreateObject () method of the ASP server object.

• Invoke the method of the component using the appropriate method parameter variable.

• Assigns the string value returned from the VB method to a variable in the ASP variable.

• Then use the variable in the Response.Write () method to send the string to the browser.

We will use some code in the ASP file to instantiate the class of the VB component, the following is the code instantiated by the VB component:

Set objreference = Server.CreateObject ("Exampleproject.exampleclass")


The ASP Server object's CreateObject () method returns the address of the VB code object, so we can invoke any public method of the class in the ASP. It should be noted that the method parameters of the ASP CreateObject () method are the VB engineering and the name of the class, objreference the reference to the object instance of the class that holds the component.

Ways to use components in ASP files

Now, we can use the component's class method Examplemethod to get an indication of the lifetime of a person in a day meter. The following code uses the value of the parameter and assigns the value of the string returned from the method to a variable named Strmethodreturn:

Strmethodreturn = Objreference.examplemethod ("Eric Clapton", True)


Tip: When our component is instantiated, Objreference represents the exampleproject.exampleclass that occurred in the CreateObject () method. Although we can equate Objreference.examplemethod with ExampleProject.ExampleClass.ExampleMethod (), we cannot use it this way.

Of course, we can also use a variable rather than a direct value as the parameter of the method, the name of the parameter variable should not be the same as the VB Method parameter table, they as long as the parameter table in the number of optional parameters, type, order the same.



Aspname = "Eric Clapton" aspage = aspemphasis = True Strmethodreturn = Objreference.examplemethod (Aspname, Aspage, AspE Mphasis)


Using variable substitution values makes the code clearer and more manageable, especially if the code becomes very long.

Now we simply return Strmethodreturn to the browser that accesses the ASP code in the ASP Response.Write () method. Here is the complete ASP code, at the end of the code, we added a line of code to detach the Component object address to clear the component's object code:



<%
'/////instantiation of Component objects
Set objreference = Server.CreateObject ("Exampleproject.exampleclass")

'/////set as a local variable for a method parameter
Aspname = "Eric Clapton"
Aspage = 56
Aspemphasis = True

'/////the method of calling the component, storing the return value
Strmethodreturn = Objreference.examplemethod (Aspname, Aspage, aspemphasis)

'/////sends the return value to the visiting browser
Response.Write (Strmethodreturn)

'/////the object that clears the component
Set objreference = Nothing
%>


Storing the above ASP code in an ASP file will produce the following string output:

Eric Clapton is over 20440.

Enabling ASP code to invoke DLL files

The test for our component is to let Windows know where it is stored and when the ASP code calls it. First, in the VB menu select the "Run" icon or "Run/start", VB will temporarily register the component to the system.

Tip: You cannot load an ASP file directly using the browser as if you were loading an HTML file, and the ASP file must be loaded onto the browser via a Web server.

The browser will display the "Eric Clapton is over 20440". The words.

In order for the component to run on another server, it must be compiled into a DLL file and then registered on the server. Of course, if you want to use the component permanently on the computer you are developing, you need to compile and register. The only file that runs the component on another computer is the compiled DLL file, which, of course, requires that the computer already has the VB Run-time Library file installed.

How to compile the component source code to get the DLL file and how to register the component on the computer is not the scope of this article, we will not describe it in detail.

As a by-product of writing IIS server-side components, we can invoke the method of writing components from any ASP file and other VB components, which further increases the flexibility and modularity of your code.


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.