Create an ASP server-side component using VB
This article describes how to call VB components in the ASP code by comparing traditional design methods. In this article, we assume that readers have relevant entry knowledge of VB and ASP.
Comparison of server-side components and client components
The server-side components and client components have many differences. The server-side component is a DLL file registered on a computer server, and the client component is registered on a computer running on the browser. In IE, these client components are referred to as ActiveX browser plug-in components.
ActiveX client components can be written using VB and send it to the browser via the Internet or intranet to generate a very exciting effect. The problem is that the ActiveX client component is limited to IE, and the server-side components written using VB can generate pure HTML code for all browsers. The biggest problem with server-side components is that this component must be run in a Windows IIS environment or in an API-compatible application in the IIS. Compared to comparison, this compatibility in the server seems to be more easier.
IIS server-side components reside in the same memory space as IIS and ready to prepare a call to the ASP web page processed on the server. In theory, we can insert any text or code in the ASP code returned to the browser, but in general, most server-side components are used to handle calculation or database information that requires a lot of time, and then the resulting The result is returned to the browser in the situation of HTML code.
Analysis of VB components
Since this article is intended to discuss the basic method of writing VB components, it will be very simple in the case where the problem can be explained. We will first analyze the VB components in detail before writing VB components.
When using VB to write server-side components, there are three hierarchical concepts (used in VB and ASP code) Need to pay attention:
· Project name
· Class name
· Method name
The name of the VB project is the Project name. Many developers regard Project names as component names, but VB only looks like is the name of the project. In our example, the Project name is ExampleProject, of course, we can name our own project name; Class name name is ExampleClass, the Method name is ExampleMethod.
The project name (component name) can also be the name of the DLL file obtained by the component code, the DLL file will contain IIS to return to the browser to return text or HTML code to the browser or the compiled VB code.
The method name refers to the VB code section that manages the specific code function, such as a check dates or list of all authors in the database. The component method has a bit corner of black box that returns specific information based on the information of the input or the input information. In general, there may be multiple methods in one component. In order to manage components more efficiently, the method can be combined with a similar classification, which is the role of the component class.
The component class can generate a copy of the component class code in memory. When using an ASP code to create an object, it is also called an object, which is instantiated. Once there is an object reference to the component class code instance, we can call the methods included in the class from the ASP code.
In our example, engineering, class, method names will be used to instantiate VB components in the ASP code, and transmit values to VB code from the ASP code in the form of method parameters, and receive from the VB method in the ASP code. The returned value.
Call VB components from ASP files
The ASP file we use to call VB components will use object variables to save the reference to the VB object. In the 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 engine name and class name (ExampleProject and ExampleClass) of the component when instantizing the VB component. Instantiate the ASP code of the VB component:
Set objreference = server.createObject ("eXampleproject.example")
The VB component will receive a value of 3 variables from the ASP code and return a value to the ASP code, which stores in the ASP variable of the name Strmethodreturn. The following code shows how the ASP code gets the value returned by the VB component. It transmits three names to the VB method to the three parameter values of PARAM1, PARAM2, and PARMA3, respectively:
StrmethodReturn = Objreference.exampleMethod (param1, param2, param3)
Param1, Param2, Param3 These three parameters must be exactly the same as the definition of methods in the VB component, the following is the class of the second line to instantiate the VB component, and call the class method to get an example of the ASP code of the return value:
Set objreference = server.createObject ("eXampleProject.example") strMethodReturn = Objreference.examplemethod (param1, param2, param3)
The following chart visually shows the engineering, classes, and method names of the VB component, how to coordinate with the components in the ASP file. When you gradually learn how to prepare the VB code and ASP files in an example, you can use the following chart as a reference.
The role of VB method
Our simple VB components will receive the user's name and age, then return a child's age, and there is an option to remind a user that it has exceeded 45 years old.
If we deliver a fictional eric clapton to the component as the first parameter value of the method, set the second parameter to 56, we will get the following return string:
Eric Clapton Is over 20440 Days Old.
If we set an optional third parameter to true (this parameter will make the method to determine if the user has more than 45 years old, we will get the following return string:
Eric Clapton Is over 20440 Days Old.
Due to the use of three complete variables, the user's name, age, and whether they have more than 45 years old, so 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-type variable named Strname to represent the user's name. The name of INTAGE is an integer variable that represents the user's age, the name of BLNAGEEMPHASISON indicates whether the user has exceeded 45 years.
Three method parameters (variables transmitted to the method code of the VB component):
Strname (String) INTAGE (Integer) BLNageemPhasis (Boolean)
Create server-side components in VB
After starting VB, double-click the "ActiveX DLL" icon in the New Project window. Once VB loads new ActiveX DLL project, you will see two open windows: Engineering Windows and Properties Windows. If there is a window that does not display it, you can select the "View" menu item from the VB menu (separately using "View" -> "Engineering Manager", "View" -> Properties Window "). Since VB is the default naming of the first project and class is Project1, Class1, we can change them to ExampleProject and ExampleClass. The modification of the project name can be made in the engineering window. There is a small box on the left side of the newly input project name in the engineering window. If the number is displayed, select the small box, number will become - numbers, the default class name (Class1) will appear below the project name. Select the default class name in the engineering window, and modify the default class name to ExampleClass in the Properties window.
When saving the project, the VB will save the code containing the class in a file that extension CLS, the extension of the project file is VBP, where the project is stored, filename, and file storage.
Server-end component properties
The properties of the ExampleClass class are displayed in the properties window. Note that the value of the instancing property is "5 MultiUse", if the type of project is set to the standard EXE project, the value of this property will change.
Select "Project" -> "ExampleProject Properties" in the VB menu, the engineering properties window is displayed. The value of the "Thread Mode" attribute at the lower right of the "General" tag should be set to "Unit Thread", which will enable multiple accessors to use different instances of our component classes simultaneously. In addition, select "Unattended Execution" and "Resident Memory" two options to avoid memory leaks in VB6.
VB method code
Now we need to use the VB's code window to enter the VB code. If the code window is still blank, then enter the code below:
Option expendit 'It will require us to define all variables. Public Function ExampleMethod (Byval Strname As String, _BYVAL INTAGE AS INTEGER, _ OPTIONAL BYVAL BLNAGEMPHASON As Boolean = false AS String
In the code above, we define the method into a public function, which means that any code outside the component can call it, because it is a function, it will return a value to the code called it.
Public Function ExampleMethod () AS STRING
The above code indicates that the ExampleMethod () function will return a value of a string type to its caller.
Our VB method has three parameter variables accepted from the ASP code, and the last parameter variable is optional. All parameter variables that receive values from the VB component need to define and use between parentheses of the VB method, we can use this way as a variable that is defined as method parameters in this way as the variables defined within the method, both The unique difference is the outside ASP code to determine their value.
Here are three variables and their data types:
BYVAL STRNAME AS STRING BYVAL INTAGEEMPHASON As Boolean = FALSE
The above code defines the data type of three method parameters, indicating that they are transmitted by value, and the third parameter is optional. If there is no third parameter, its default is false. Then, we will add some necessary commas, spaces, and underline (_) in the definition of the method to comply with the grammar requirements of VB. We will put the parameter list in the middle of the parentheses defined, the method obtained is defined as follows:
Public Function ExampleMethod (Byval Strname As String, _BYVAL INTAGE AS INTEGER, _ OPTIONAL BYVAL BLNAGEMPHASON As Boolean = false AS String
In the VB's code window inputs the above method definition, an end function statement will generate. The definition of the method and the end function are where we write your own code.
The first line of code we added in the body of the method is to define a string variable to store the string data returned by the method. We can use string variables to return text data to code to the calling method.
DIM STRRETURNSTRING AS STRING
Below we can create a returned string. We can use the strname variable value transmitted by the ASP code with the parameter list of the method. First connect the Strname parameter variable value to the string "is over". Next we will calculate a survive number of days using the intage parameter variable, then add an "Age in Days" string on the previous string. It should be noted that we need to convert the product of the INTAGE * 365 to a string before you can combine it in the Strreturnstring string, and the CSTR () method in the VB can achieve this.
Strreturnstring = Strname & "Is over" & cstr (intage * 365)
If you assume that the name is Eric Clapton from the ASP code, the age parameter is 56, so Strreturnstring should contain the following:
Eric Clapton Is Over 20440
Our final string will be added according to the value of the intage variable over 45 and the BLNAGEEMPHASISON variable is set to TRUE and add it.
"Days old" or "days old". The following code can implement this feature:
If BLNAGEEMPHASISON AND = Strreturnstring & "DAYS OLD." Else Strreturnstring = Strreturnstring & "Days Old." End IF
If the ASP code does not use the value of the BLNAGEEMPHASISON variable to send to the component, according to our method, its value will be set to false by default. 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 Days Old.
Otherwise, we will 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 assume the value of the string to the name of the method:
EXAMPLEMETHOD = STRRETURNSTRING
Complete method code as follows: Public Function ExampleMethod (ByVal strName As String, _ ByVal intAge As Integer, _ Optional ByVal blnAgeEmphasisOn As Boolean = False) As String '/ establish local variable Dim strReturnString As String' variable / create returned value strReturnString = strName & "is over" & CStr (intAge * 365) '/ sound strReturnString If blnAgeEmphasisOn And intAge> 44 Then strReturnString = strReturnString & "days OLD." Else strReturnString = strReturnString & "days old." End If' / Return Strings EXAMPLEMETHOD = STRRETURNSTRING End Function
Call the VB method in the ASP code
Instantiate VB objects in ASP code
Most of the ASP code we need are discussed in the previous A Conceptual Overview. In the ASP code, we still need to complete the following work in subsection:
· Instantiate VB components using the CreateObject () method of the ASP Server object.
• Method of calling the component using the appropriate method parameter variable.
• Assign a variable in the ASP variable to the string value returned from the VB method.
· Then use this variable to send a string to your browser in the response.write () method.
We will use some of the code in the ASP file to instantiate the class of VB components. The following is the code instantiated by VB components:
Set objreference = server.createObject ("eXampleproject.example")
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 the ASP. It should be noted that the method parameters of the ASP CreateObject () method are the names of the VB engineering and classes, ObjReference used to hold the object instance of the class's class.
Method for using components in ASP files
Now, we can use the components of the class method ExampleMethod to get a life that indicates a person's day. The following code uses the value of the parameter and will assign the value of the string returned from the method to the variable of Strmethodreturn:
StrmethodReturn = Objreference.exampleMethod ("Eric Clapton", 56, True)
Tip: After our components are instantiated, Objreference represents ExampleProject.exampleClass in the CreateObject () method. Although we can regard Objreference.exampleMethod as the equivalent to ExampleProject.exampleClass.exampleMethod (), we can't use this.
Of course, we can also use variables instead of direct value as a parameter, the name of the selected parameter variable does not need to be the same as the VB method parameter table, as long as the number, type, type, The order is the same. askNAME = "Eric clapton" aspage = 56 aspemphasis = true strmethodreturn = objreference.examplemethod (askNAME, Aspage, Aspemphasis)
Use variable replacement values to make the code more clear, more manageable, especially when the code has become long.
Now let's return StrMethodReturn to a browser that accesses the ASP code in the ASP response.write () method. The following is a complete ASP code. At the end of the code, we add a line of code to the component object address to clear the component's object code:
<% '/ Instantiation component object set objreference = server.createObject ("eXampleProject.examples")' / set as a local variable as method parameter aspname = "Eric Clapton" ASPAGE = 56ASPEMPHAS = true '/ calling components method, storage return Value strMethodreturn = Objreference.exampleMethod (aspname, aspage, aspemphasis) '/ Send the return value to the accessible browser Response.write (StrMethodReturn)' / Clear Components Object Set Objreference = Nothing%>
Store the above ASP code in an ASP file, will produce the following string output:
Eric Clapton Is over 20440 Days Old.
Make the ASP code to call the DLL file
Testing to our components is to let Windows know its storage location and when the ASP code is called. First, select the "Run" icon or "Run / Start" in the VB menu, and VB will temporarily register the component to the system.
Tip: You cannot load the ASP file directly as you load the HTML file, and the ASP file must be loaded into the browser via the web server.
The browser will display the words "Eric Clapton Is Over 20440 Days Old.".
In order to enable the component to run on other servers, it must be compiled into a DLL file and then register on the server. Of course, if you want to use the component permanently in the development, you also need to compile, register. The only file that runs the component on other computers is the compiled DLL file, of course, this requires a computer that the computer has already installed VB running time library files.
How to compile component source code to get a DLL file and how to register components on your computer is not a discussion scope of this article, we will no longer be described in detail.
As a by-product of the IIS server-side component, we can call the modified components from any ASP file and other VB components, which will further increase the flexibility and modularity of the code.