Create | objects
Creating an instance of a script Run-time library object is exactly the same as instantiating any other object and component. You can use the CreateObject method provided by the ASP server object (make sure that the object is created within the context of the current page), or use a <OBJECT> element. We're going to look at both approaches, and the way we do that depends on the needs of the page.
5.2.1 Use Server.CreateObject method
As you can see in the study of the server object, components or other object instances are created according to their ProgID:
<%
Dim Objthis
Set objthis = Server.CreateObject ("ADODB. Connection ")
%>
The ProgID string "formal" format is "vendor. Component. Version", and the vendor's name and version are optional. Typically, the ProgID contains only the first two parts (as in the previous example). A few vendors set the version number in ProgID, which avoids using the same ProgID for newer, backward-compatible versions, which requires changing the ASP page to use the new version.
5.2.2 Use <OBJECT> elements
You can use the standard html<object> element to create a component instance on the server by adding the runat parameter and specifying its value as "server." In addition, it is usually a classid that provides an object's ProgID string instead of a number:
<object id= "Objthis" runat= "SERVER" progid= "This.object" >
<param name= "param1" value= "value1" >
<param name= "param2" value= "value2" >
</OBJECT>
If the object in the script above has the appropriate properties to use in the script, the <OBJECT> element can be set through the <PARAM> element, as it usually does in an HTML page. The CodeBase property is not required when using the <OBJECT> element in ASP, and the server does not attempt to download and install objects or components when it is not available.
1. Specify a ClassID
Alternatively, you can specify the ClassID of the object or component you want to create. This is useful when you do not know what other components are installed on the target machine. For example, when instantiating a component on the page of a browser on the client.
In theory, the ProgID (text "Vendor. Component") of a component should not conflict with each other and should be unique. However, this is not unassailable. It is possible that a supplier in the north of the United States has the same name as a supplier on a Greek island. However, when you use ClassID to recognize access, the same name does not occur because ClassID is unique.
If you decide to use the ClassID of an object or component, you should put it in the ClassID property instead of the ProgID property. Such as:
<object id= "Objthis" runat= "SERVER"
Classid= "CLSID:892D6DA7-E0F9-11D2-B2E9-00105A42AF30" >
<param name= "param1" value= "value1" >
<param name= "param2" value= "value2" >
</OBJECT>
However, when instantiating objects on your own server, you should know how objects and components are installed. This makes it safe to use ProgID when creating an object instance in ASP code. This is why ClassID is rarely used within an ASP page. However, because ProgID is used to find ClassID, the classid of a component or object can be substituted for ProgID if you wish.
2. Set the scope of an object instance
By default, objects created in all ASP pages and component instances (either with Server.CreateObject methods or <OBJECT> elements) have scopes within the page (page scope). This means that objects and components only exist when the page is running on ASP, and are automatically canceled when the page is finished and the results are sent to the client.
However, if you place a <OBJECT> declaration in a Global.asa file (which exists in the root of a site or virtual application), you can designate the scope of an object or component as an application or session scope.
(1) Creating objects in the application-tier scope
Create an application-layer scope object by setting the scope property to "application":
<object id= "Objthis" runat= "SERVER" progid= "This.object"
Scope= "Application" >
</OBJECT>
An object instance was created at the beginning of the application, that is, once the user requests a page from the virtual application's directory, the object instance is created. For the default Web site, this can be the any directory on the site. Until the end of the application (end of the final user session), the object instance persists and can be referenced and used by any user within any page of the virtual application or site directory.
(2) Creating objects in the session-level scope
If you want to create an instance of an object that is used by a single user whose scope is all the pages that he accesses, you can create a session-level scope object. This is accomplished by setting the Scope property to session:
<object id= "Objthis" runat= "SERVER" progid= "This.object"
Scope=session ">
</OBJECT>
The object is created once it is referenced by the user from the program code in the page loaded from the virtual application or site (there are <OBJECT> declarations in the Global.asa file). When the user's session life cycle ends and is canceled, the object instance it references is canceled.
(3) About scopes and states
Making the scope of an object instance global or for a user session Global environment may seem like a good idea, but there are some issues to consider when you actually use it, one of which is the ability to effectively protect the state of the object between many of the users ' requests. In other words, you can set some properties of objects that are common to all the pages you use. This seems like a good idea because you don't have to create a new instance every time and set its properties.
In fact, Microsoft's recommendation not to do so in general, the idea is the remnants of traditional programming ideas. The biggest problem facing the Web is how servers and Web applications and the dynamic Web pages that are provided cope with millions of site visitors. A component instance resides in memory waiting for a page request from a specific user, and for a site that may have hundreds of users browsing at the same time, this does not effectively use resources.
Windows 2000 provides a new COM + run-time feature that can handle the creation, caching, and use of components, in a way that maximizes throughput but minimizes server resources. The problem of where and how long an object instance is stored is best done by the operating system itself, not by the programmer.
That is, you create an object instance on the page where you want it to disappear when the page terminates. COM + collates These fragments and automatically handles some of the complex work behind the scenes. If you want to learn more about this, chapter 14th studies the creation of components in more detail.
Of course, in some cases, we may require an object to have the scope of the application layer and session layer, especially if the state is saved between page requests. When you discuss the Dictionary object later, you will have an example of this.
The difference between 5.2.3 Server.CreateObject and <OBJECT>
The Server.CreateObject method immediately creates an object instance. In most cases this is what we hope for. The <OBJECT> element creates the specified object instance only when it first references an object. Therefore, if you stop using the object in your code, the object instance is not created.
This may be useful if your code uses this object in only one case (which may depend on the value of the request parameter). Because if you do not need this object, you can save resources on the server.
However, if you definitely need to create an object, you can do so by using the Server.CreateObject method. Creating an object with the <OBJECT> element helps prevent the Server.CreateObject line in the program from being canceled when the call to the object is canceled in code, which is, of course, a careless program design.
The last thing to remember is that if the object was created using the Server.CreateObject method, you can remove the object from the session or application, but not by using the <OBJECT> element.
5.2.4 Component Threading Model
Another issue that should be considered when using objects or components within a page is the behavior of the object involved in responding to multiple requests. In fact, in the ASP, this is one of the most complex topics to understand. The threading model of a component, combined with its scope, affects the performance and efficiency of the component and application, and also affects the ASP pages that instantiate it.
A thread is a system object that is executed by the processor to complete the tasks defined by the component code. Each thread can be considered a single binary instruction set. In a multithreaded environment like windows, multiple threads can run at the same time.
There are actually five threading models (including the neutral-threading model introduced in Windows 2000):
· single-threaded (Single thread): only one process can use a component at a time.
· apartment-threaded (unit thread): Several processes can use a component, but only one is on the specified thread.
· Neutral-threaded (Neutral thread): Several processes can use a component and can use any of the specified set of threads.
· multiple-threaded or free-threaded (multi-threaded or free threads): Several processes can use a component, and these processes can run on different threads.
· both-threaded (Dual thread): An object can be both a unit thread and a free thread.
The technical details of the threading model are not explained here, and the corresponding content is available later in this book.
A component of a cell thread, such as a component created with Visual Basic or as an XML script, can run well within a page-layer scope and is acceptable within a session-level scope. In fact, at the page level, because of the lower data processing overhead, it is also possible to run two-threaded components well.
The model of the neutral threading in Winodws 2000 even provides better performance, although so far there are few such components and the development tools that are compatible with them.
If you need a session-level component, use a two-threaded component that is available. And if you need an application-layer scope, you can always use a two-threaded component.
However, Microsoft recommends avoiding the use of session-level scoped components, or even the application-layer scoped components, unless they are absolutely necessary. Making a component more active than the time required for a component with a page-level scope is not beneficial for objects that provide agent attributes for COM +. 5.2.5 Reference Object type library
In an earlier ASP version, when an object or component was used in a script, the public constants defined within the component, if any, would no longer be valid in the ASP. This means that we need to declare them (or equivalent) ourselves and specify the corresponding values.
For example, when using an ActiveX database object (ADO) component in an earlier version of ASP (which will be studied in detail in chapter 8th), you have to add a predefined constant declaration with the recordset's Open method. For example:
Const adOpenKeyset = &h0001
Const adlockpessimistic = &h0003
Const adCmdTable = &h0002
...
Rs. Open "Contact", "dsn=globalexampledata; Uid=examples; password=; ", _
adOpenKeyset, adLockPessimistic, adCmdTable
...
Alternatively, use the #include directive to insert a file named Adovbs.inc on the page. This file is provided by iis/asp and contains all predefined constants required by ADO. When you update your code, you must confirm that you are using the latest version and check that it is available for all page requests.
For IIS 5.0, there is a better way to add references to the type library of a component or object by using the metadata directive within an HTML annotation element (IIS 4.0 does not support this feature).
<!--METADATA type= "TypeLib"
File= "Path_and_name_of_file"
Uuid= "Type_library_uuid"
verssion= "Major_version_number.minor_version_number"
Lcid= "locale_id"
-->
which
· Path_and_name_of_file is the absolute physical path of a type library file (. tlb) or an ActiveX DLL, which must either be supplied with this parameter or a type_library_uuid parameter.
· Type_library_uuid is the only unique identifier for the type library, which must either be supplied with this parameter or a path_and_name_of_file parameter.
· Major_version_number.minor_version_number (optional) defines the version of the desired component. If this version is not available, the most recent version is used.
· LOCALE_ID (optional) is an area marker. If no type library is found in this area, the computer uses the default (defined by installation) zone.
Therefore, using this technique, the built-in ADO predefined constants can be made available on an ASP page by using the following code:
<!--METADATA type= "TypeLib"
file= "C:Program FilesCommon FilesSystemadomsado15.dll"
-->
The filename Msado15.dll can also be used for later versions of the ADO component (after 2.50).
If the ASP cannot mount the type library, it returns an error and stops execution of the page. Possible error hints are shown in table 5-1:
Table 5-1 Error hint code and description
Error description
Invalid type library description for ASP 0222
Type library not found in ASP 0223
The ASP 0224 type library cannot be loaded
ASP 0225 type libraries cannot be packaged (that is, ASP cannot create type library wrapper objects from the specified type library)
5.2.6 create an object instance on the client
When discussing techniques for instantiating objects and components on a server in ASP, it is worth emphasizing the way in which the client pages are run in the browser to do the same work. This is useful if you use ASP to create a page that contains client-side scripting, or to create a client component instance using the <OBJECT> element. In most cases, the script runtime object can be instantiated and used on the client, the same effect as the ASP on the server.
1. VBScript CreateObject method
When the client uses CreateObject, a component or object instance is created within the browser's environment, which runs in the same memory space (within the process) as the browser, unless the implemented object is an executable file with the. exe extension file name.
You typically specify the ClassID of an object instead of using a ProgID string, which can not conflict with other objects installed on the client.
<script language= "VBScript" >
Dim Objthis
Set Objthis = CreateObject ("CLSID:892D6DA7-E0F9-00105A42AF30")
...
</SCRIPT>
Of course, you can use ProgID and use common objects or components (especially those provided by standard installations), so the risk of getting the wrong component is small:
<script language= "VBScript" >
Dim Objthis
Set Objthis = CreateObject ("Scripting.Dictionary")
...
</SCRIPT>
2. Jscript ActiveXObject method
In order to instantiate the objects and components of JScript on the client, you must use the ActiveXObject method and the new operator:
<script language= "JScript" >
var objmydata = new ActiveXObject (' clsid:892d6da7-e0f9-00105a42af30 ');
</SCRIPT>
Or:
<script language= "JScript" >
var objmydata = new ActiveXObject (' This.object ');
</SCRIPT>
3. <OBJECT> element Technology
You can also use the <OBJECT> element to create an instance of a client object or component. You should omit the runat attribute or set it to "CLIENT". However, this property is ignored on the client, so the sole purpose of setting this property is to prevent confusion when an ASP page instantiates a server-side component instance using the <OBJECT> element.
<object id= "Objthis" runat= "CLIENT"
Classid= "CLSID:892D6DA7-E0F9-00105A42AF30"
codebase= "Http://yourserver.com/components/mycomponent.cab" >
<param name= "param1" value= "value1" >
<param name= "param2" value= "value2" >
</OBJECT>
Note that the CODEBASE attribute appears here, indicating that the component from the URL is allowed to download and install if it is not installed. IE more than 3.0 of the version has this function.
You can view Web site http://msdn for methods that use the <OBJECT> element, properties you can use, and values that you use on the client. Microsoft.com/workshop/author/dhtml/reference/objects/object.asp, or <OBJECT> in the Windows Platform SDK documentation tags, or see "IE5 Dynamic HTML Programmer ' Reference" a book, ISBN 1-861001-74-6,wrox publishing house.