Create asp server-side components using vb
This article introduces how to call vb components in asp code by comparing with traditional design methods. In this article, we assume that the reader has relevant introductory knowledge of VB and ASP.
Comparison of server-side components and client-side components
There are many differences between server-side components and client-side components. Server-side components are dll files registered on the computer server, and client-side components are registered on the computer where the browser is running. In IE, these client-side components are called activex browser plug-in components.
ActiveX client components can be written in VB and sent to the browser through the Internet or intranet to generate wonderful effects. The problem is that activex client-side components are limited to ie, while server-side components written in vb can generate pure html code and are suitable for all browsers. The biggest problem with server-side components is that the component must run in a Windows+IIS environment or an application compatible with IIS's API. In comparison, it seems easier to achieve this compatibility on the server side.
The IIS server-side component resides in the same memory space as IIS and is always ready for calls to ASP web pages processed on the server. In theory, we can insert any text or code in the ASP code returned to the browser, but generally speaking, most server-side components are used to handle calculations or database information lookups that require a lot of time, and then the resulting The result is returned to the browser in the form of html code.
Analysis of vb components
Since this article aims to discuss the basic methods of writing VB components, the examples will be very simple if they can illustrate the problem. Before discussing writing VB components in detail, we will first conceptually analyze VB components.
When using VB to write server-side components, there are three hierarchical concepts (used in both VB and ASP code) that need to be paid attention to:
·project name
·class name
·method name
The name of the VB project is the project name. Many developers regard the project name as the component name, but VB only regards it as the name of the project. In our example, the project name is exampleproject. Of course, we can name our own project arbitrarily; the class name is exampleclass, and the method name is examplemethod.
The project name (component name) can also be the name of the dll file compiled from the component code. The dll file will contain the compiled vb code used by iis to return text or html code to the browser.
The method name refers to the portion of Visual Basic code that manages a specific code function, such as calculating a date or displaying a list of all authors in a database. Component methods are kind of black boxes that perform specific work or return specific information based on input information. Generally, there can be multiple methods in a component. In order to manage component methods more effectively, methods can be grouped together according to similar classifications. This is the role of component classes.
The component class can generate a copy of the component class code in memory. When using asp code to create an object, it is also called an object. This is instantiation. Once we have an object reference to the component class code instance, we can call the methods contained in the class from the asp code.
In our example, the project, class, and method names will be used to instantiate the VB component in the ASP code, and pass values from the ASP code to the VB code in the form of method parameters, which are received in the ASP code from the VB method. The value returned.
Call vb component from asp file
The asp file we use to call the vb component will use object variables to save references to the vb object. In an asp file, you can create an object using the createobject() method of the asp server object, which will return a reference to the object it created. In the example, we will use objreference as the object variable of the component. The following code shows that the asp code needs to use the component's project name and class name (exampleproject and exampleclass) when instantiating the vb component.
ASP code to instantiate vb component:
set objreference = server.createobject(exampleproject.exampleclass)
The vb component will accept the values 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 gets the value returned by the vb component. It passes three parameter values named param1, param2 and parma3 to the vb method:
strmethodreturn = objreference.examplemethod(param1, param2, param3)
The three parameters param1, param2, and param3 must be exactly the same as the definitions of the methods in the VB component. The following is an example of two lines of ASP code that instantiates the class of the VB component and calls the class method to obtain the return value:
set objreference = server.createobject(exampleproject.exampleclass)
strmethodreturn = objreference.examplemethod(param1, param2, param3)
The chart below visually shows how the project, class, and method names of the VB component are coordinated with the component instantiation code in the ASP file. You can use the chart below as a reference as you learn step by step how to write the vb code and asp files in the example.
The role of vb method
The simple VB component in our example will get the user's name and age, and then return the user's age in days, and there is an option to remind a user whether he is over 45 years old.
If we pass a fictional eric clapton to the component as the first parameter value of the method, and set the second parameter to 56, we will get the following return string:
eric clapton is over 20440 days old.
If we set the optional third parameter to true (this parameter will make the method determine whether the user is over 45 years old), we will get the following return string:
eric clapton is over 20440 days old.
Since three completely different variables are used - the user's name, age and whether they are over 45, we need to use three method parameters to transfer this information from the ASP file to the VB code. In VB, it is very important to consider which data types to use. We will use a string variable named strname to represent the user's name, an integer variable named intage to represent the user's age, and a Boolean variable named blnageemphasison to indicate whether the user is over 45 years old.
Three method parameters (variables passed to the method code of the vb component):
strname (string)
intage (integer)
blnageemphasisison (boolean)
Create server side components in vb
After starting vb, double-click the "activex dll" icon in the "New Project" window. Once VB loads the new activex dll project, you will see at least two open windows: the project window and the properties window. If a window cannot be displayed, you can select the "View" menu item from the VB menu (use "View" -> "Project Manager", "View" -> "Properties Window" respectively).
Since the default names of the first project and class in VB are project1 and class1 respectively, we can change them to exampleproject and exampleclass respectively. The project name can be modified in the project window. There is a small box with + or - to the left of the newly entered project name in the project window. If the + sign is displayed, select the small box, the + sign will turn into a - sign, and the default class name (class1) will be displayed below the project name. Select the default class name in the project window, and change the default class name to exampleclass in the properties window.
When saving the project, VB will save the code containing the class in a file with the extension cls. The extension of the project file is vbp, which stores various settings of the project, file name and file storage location.
Property values for server-side components
Display the properties of the exampleclass class in the properties window. Note that the value of the instancing property is "5 multiuse". If the project type is set to a standard exe project, the value of this property will change accordingly.
Select "Project" -> "ExampleProject Properties" in the VB menu, and the project properties window will be displayed. The value of the "Threading Mode" property at the lower right of the "General" tab should be set to "Apartment Threading", which will enable multiple visitors to use different instances of our component class at the same time. In addition, select the two options of "Unattended Execution" and "Resident Memory" to avoid memory leaks in VB6.
vb method code
Now we need to use the vb code window to enter vb code. If the code window is still blank, enter the following code:
option explicit
'It will require us to define all variables.
public function examplemethod(byval strname as string, _
byval intage as integer, _
optional byval blnageemphasisison as boolean = false) as string
In the above code, we defined the method as a public function, which means that any code outside the component can call it. Since it is a function, it will also return a value to the code that calls it.
public function examplemethod() as string
The above code indicates that the examplemethod() function will return a string type value to its caller.
Our vb method comes with 3 parameter variables that accept values from asp code, the last parameter variable is optional. All parameter variables used to receive values from outside the VB component need to be defined and used between the brackets of the VB method. We can use variables defined in this way as method parameters just like variables defined within the method. The only difference between them is the outside asp code that determines their values.
Here are the three variables and their data types:
byval strname as string
byval intage as integer
optional byval blnageemphasis as boolean = false
The above code defines the data types of the three method parameters, indicating that they are passed by value, and the third parameter is optional. If there is no third parameter, its default value is false.
Then, we will add some necessary commas, spaces and underscores (_) in the definition of the method so that it can meet the grammatical requirements of VB. We will put the parameter list between the parentheses of the method definition, and the resulting method definition will be as follows:
public function examplemethod(byval strname as string, _
byval intage as integer, _
optional byval blnageemphasisison as boolean = false) as string
Enter the above method definition in the VB code window, and an end function statement will be generated. Between the method definition and the end function is where we write our own code.
The first line of code we add in the body of the method is to define a string variable to store the string data returned by the method. Instead of using a string variable, we can use a string to return text data to the code that calls the method.
dim strreturnstring as string
Next we can create the returned string. We can use the strname variable value passed by the asp code through the parameter list of the method. First concatenate the strname parameter variable value with the string "is over". Next we will use the intage parameter variable to calculate the number of days that a person has survived, and then add the "age in days" string to the previous string. It should be noted that we need to convert the product of two integers intage * 365 into a string before combining it in the strreturnstring string. The cstr() method in VB can achieve this purpose.
strreturnstring = strname & is over & cstr(intage * 365)
If it is assumed that the name passed to the component from the asp code is eric clapton, and the age parameter is 56, therefore, strreturnstring should contain the following content:
eric clapton is over 20440
Our final string will be added based on whether the value of the intage variable exceeds 45 and whether the blnageemphasison variable is set to true.
"days old" or "days old". The following code can achieve this function:
if blnageemphasis and intage > 44 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 will be set to false by default according to our method definition. If it is set to true and the value of intage variable is greater than 45, we will get the following output:
eric clapton is over 20440 days old.
Otherwise, we get the following output:
eric clapton is over 20440 days old.
In order to return the above string to the asp code of the calling component, we assign the value of the string to the name of the method:
examplemethod = strreturnstring
The complete method code is as follows:
public function examplemethod(byval strname as string, _
byval intage as integer, _
optional byval blnageemphasisison as boolean = false) as string
'///// Create local variables
dim strreturnstring as string
'///// Create the value of the returned variable
strreturnstring = strname & is over & cstr(intage * 365)
'///// Improve strreturnstring
if blnageemphasis and intage > 44 then
strreturnstring = strreturnstring & days old.
else
strreturnstring = strreturnstring & days old.
end if
'///// Return string
examplemethod = strreturnstring
end function
Call vb method in asp code
Instantiate vb object in asp code
Most of the ASP code we need has been discussed in a conceptual overview above. In the asp code, we still need to complete the following work in sequence:
·Use the createobject() method of the asp server object to instantiate the vb component.
·Call the component's methods using appropriate method parameter variables.
·Assign the string value returned from the vb method to a variable in the asp variable.
·Then use this 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 for vb component instantiation:
set objreference = server.createobject(exampleproject.exampleclass)
The createobject() method of the ASP server object returns the address of the VB code object, so we can call any public method of the class in ASP. It should be noted that as the method parameter of the asp createobject() method is the name of the vb project and class, objreference is used to maintain a reference to the object instance of the component's class.
How to use components in asp files
Now, we can use the component's class method examplemethod to get an indication of a person's lifespan in days. 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, 56, true)
Tip: When our component is instantiated, objreference represents the exampleproject.exampleclass that appeared in the createobject() method. Although we can think of objreference.examplemethod as equivalent to exampleproject.exampleclass.examplemethod(), we cannot use it this way.
Of course, we can also use variables instead of direct values as method parameters. The names of the selected parameter variables do not need to be the same as those in the VB method parameter list. They only need to be the same as the number, type, and number of non-optional parameters in the parameter list. The order is the same.
aspname = eric clapton aspage = 56 aspemphasis = true strmethodreturn = objreference.examplemethod(aspname, aspage, aspemphasis)
Using variables instead of values makes your code clearer and more manageable, especially when it gets long.
Now we only need to return strmethodreturn to the browser accessing the asp code in the asp response.write() method. Below is the complete asp code. At the end of the code, we add a line of code that separates the component object address to clear the component's object code:
<%
'///// Instantiate component object
set objreference = server.createobject(exampleproject.exampleclass)
'///// Set local variables as method parameters
aspname = eric clapton
aspage=56
aspemphasis = true
'///// Call the component's method and store the return value
strmethodreturn = objreference.examplemethod(aspname, aspage, aspemphasis)
'///// Send the return value to the visiting browser
response.write(strmethodreturn)
'///// Clear the component object
set objreference = nothing
%>
Storing the above asp code in an asp file will produce the following string output:
eric clapton is over 20440 days old.
Enable asp code to call dll files
The test of our component is to let windows know where it is stored and when the asp code calls it. First, select the "Run" icon or "Run/Start" in the VB menu, and VB will temporarily register the component with the system.
Tip: You cannot use the browser to load ASP files directly like loading html files. ASP files must be loaded into the browser through the web server.
The browser will display the words "eric clapton is over 20440 days old."
In order for the component to run on other servers, 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 development computer, you also need to compile and register it. The only file required to run this component on other computers is the compiled dll file. Of course, this requires that the computer has the vb runtime library file installed.
How to compile the component source code to obtain the dll file and how to register the component on the computer are beyond the scope of this article, so we will not describe it in detail.
As a by-product of writing IIS server-side components, we can call the methods of the written components from any ASP file and other VB components, which will further improve the flexibility and modularity of the code.