Chapter 4 Library objects during script Runtime

Source: Internet
Author: User
Chapter 4 Library objects during script Runtime
The previous section describes how ASP can use an instance of an object defined on the server to fully utilize the provided methods and attributes to expand ASP performance. There are a series of objects available for use, including Script objects and standard IIS/asp installed components, as well as self-created or purchased objects from other vendors. You can also download objects from various websites on the Internet for free and use them on your own pages.
This chapter will discuss the objects generally referred to as the scripting Runtime Library provided by the ASP script environment. These objects are provided Code , And ASP scripts Program Complete Multiple practical tasks together.
Another component is the active server component, which is implemented through a separate ActiveX DLL file or other files. The following sections will discuss the relevant content.
Of course, you need to study how to use these objects on the page. In the previous chapter, we have learned how the server provides a method to instantiate an object. This chapter will discuss this in depth.
This chapter introduces the following:
? What does the Script Engine provide as a script object.
? How to Create a script object and other component instances.
? Summary of the members and attributes of the script object.
? How to Use Script objects in code.
The following describes the definition of the script object.

5.1 script object definition
The ASP object model is studied in the previous chapter.
Object Model is a basic means to understand the relationship between various parts of the system.
The ASP object model provides a structure to manipulate HTTP requests, responses, and different elements in the ASP environment as a whole. For example, we have seen how to obtain any cookie value from the browser by viewing the cookie set of the ASP request object.
The scripting language we use also has an object model. However, this object model provided by the scripting language is different from the object model directly provided by asp dll. The script object is run by the Microsoft Script Runtime Library (scrrun. DLL). When the default Active Scripting script engine is installed, the Microsoft Script Runtime Library is also installed.

5.1.1 different types of objects and components
Don't be confused about the terms "object" and "component". They can all be part of ASP within a certain range and can also be accessed through COM. In terms of concept, they can be divided into four categories:
? ASP built-in objects, such as objectcontext, request, response, application, session, server, and asperror. From Chapter 1 to Chapter 2 of this book, I have studied these contents.
? Script object. Used by the library during the script runtime, such as Dictionary, filesystem, and textstream. This is the object to be discussed in this chapter.
? Installable components. Provided by Microsoft during standard installation of IIS 5.0 and ASP 3.0. This will be discussed in the next chapter.
? Other components. Components purchased from other independent vendors, discovered on the website, or created on their own. There are also some other components provided by Windows services or products, such as Windows Scripting host. A list is provided in the appendix of this book. This book has some chapters dedicated to describing how to build your own components.

5.1.2 VBScript and JScript objects
As part of the script Runtime Library, Microsoft provides three main objects:
? A dictionary object provides an extremely useful storage object used to store values and access and reference through the object name rather than its index. For example, it is very suitable for storing the name/value pairs retrieved from the ASP request object.
? The FileSystemObject object provides access to the underlying File System of the server (IE 5.0 is used on the client and used together with a special page named "hypertext application (HTA ). The FileSystemObject object can be used to traverse the local and network drives, folders, and files of the computer.
? The textstream object provides access to files stored on the disk and is used in collaboration with the FileSystemObject object. Textstream objects can read or write text (ordered) files, and can only be instantiated through FileSystemObject objects. Therefore, textstream objects are often considered as sub-objects of FileSystemObject objects.
The FileSystemObject object is a series of other "parent" objects and collections used to interact with the file system ". This object provides three sets of objects: drives, folders, and files. Each set is a collection of corresponding drive, folder, and file objects. They are used to traverse and locate the drive, folder (directory), and files on the disk. The relationship between objects is 5-1:
[Img] http: // dave_lu.myetang.com/asppic/asp79.jpg [/img]
Figure 5-1 Relationship between objects in the library during script Runtime
The following describes these objects and sets in sequence and how to use them. However, you must first understand the differences between the creation or instantiation methods of object instances and components. This is the main content of the next section.

5.2 create an object generation component instance
The instance for creating Library objects during the script Runtime is exactly the same as that for creating any other objects and components. You can use the Createobject method provided by the ASP Server Object (ensure that the object is created in the environment of the current page) or use an <Object> element. We will study whether these two methods depend on the needs of the page.

5.2.1 use the server. Createobject Method
As you can see when studying server objects, components or other object instances can be created based on their progid:
<%
Dim objthis
Set objthis = server. Createobject ("ADODB. Connection ")
%>
The official format of the progid string is "supplier. component. Version". The supplier name and version are optional. Generally, the progid contains only the first two parts (in the example above ). A few vendors set version numbers in progid, which will avoid using the same progid for backward compatible new versions. This requires you to change the ASP page to use the new version.

5.2.2 use the <Object> element
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 to provide the object's progid string rather than the numerical classid:
<Object ID = "objthis" runat = "server" progid = "This. Object">
<Param name = "param1" value = "value1">
<Param name = "param2" value = "value2">
</Object>
If the objects in the preceding script have corresponding attributes that can be used in the script, you can set the attributes in the <Object> element through the <param> element, just like what is usually done on an HTML page. When an <Object> element is used in ASP, the codebase attribute is not required. When it is unavailable, the server will not try to download or install objects or components.
1. Specify a classid
In addition, 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 a component is instantiated on a browser page on the client.
Theoretically, the progid of the component (the text "supplier. component") should not conflict with each other and should be unique. However, this is not impeccable. It is possible that a supplier in northern United States has the same name as a supplier in a small Greek island. However, when classid is used to identify access, the same name will not occur because the classid is unique.
If you decide to use the classid of an object or component, you should put it into the classid attribute instead of the progid attribute. For example:
<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 an object on your server, you should know how to install the object and component. In this way, you can use progid safely when creating an object instance in ASP code. This is why classid is rarely used in ASP pages. However, because progid is used to find the classid, you can replace progid with the classid of the component or object if you want.
2. Set the scope of the object instance
By default, all objects and component instances created on the ASP page (whether using the server. Createobject method or <Object> element) have page scope ). This means that the object and component only exist when the page is running on ASP. After the page is completed and the result is sent to the client, it is automatically canceled.
However, if. the <Object> declaration is placed in the ASA file (which exists in the root directory of the site or virtual application). You can specify the scope of an object or component as the application or session scope.
(1) create an object at the application layer Scope
Create an application-layer scope object by setting the scope attribute to "application:
<Object ID = "objthis" runat = "server" progid = "This. Object"
Scope = "application">
</Object>
An object instance is created at the beginning of the application, that is, once you request a page from the directory of the virtual application, an object instance is created. For the default web site, this can be any directory on the site. The object instance exists until the application ends (the last user session ends) and can be referenced and used by any user on any page of the virtual application or site directory.
(2) create an object in the Session Layer Scope
If you want to create an object instance used by a single user and its scope is all pages accessed by the user, you can create a session-layer scope object. This is achieved by setting the scope attribute to "session:
<Object ID = "objthis" runat = "server" progid = "This. Object"
Scope = session ">
</Object>
Once an object is referenced, it is created. The reference is completed by the program code on the page loaded by the user from the virtual application or site (the <Object> declaration is included in the Global. Asa file ). When the user's session lifecycle ends and is canceled, the referenced object instance is canceled.
(3) Scope and status
It seems to be a good idea to make the scope of the object instance global or to session the global environment for the user, but some problems need to be considered in actual use, one of them is that the object state can be effectively protected between many users' requests. In other words, you can set some attributes of objects, which are shared with all pages used. Because you don't have to create a new instance and set its attributes every time, this seems a good way.
In fact, Microsoft recommends that you do not do this in general. This idea is a legacy of traditional programming ideas. On the web, the biggest challenge is how servers, web applications, and dynamic web pages can handle millions of website visitors. The component instance is resident in the memory to wait for a page request from a specific user. For websites that may have hundreds of users simultaneously browsing, this cannot effectively use resources.
Windows 2000 provides a new COM + runtime feature that can process component creation, caching, and usage. It maximizes throughput but minimizes server resources. It is best for the operating system to decide where the object instance is stored and how long it will take.
That is to say, create an object instance as needed on the page and make it disappear when the page ends. COM + sorts out these fragments and automatically handles complicated tasks in the background. To learn more about this, Chapter 14th describes how to create a component.
Of course, in some cases, we may require an object to have the application layer and Session Layer scopes, especially when the page request is saved. When we discuss a dictionary object later, there will be an example of this.

5.2.3 difference between server. Createobject and <Object>
The server. Createobject method immediately creates an object instance. In most cases, this is what we want. The <Object> element creates a specified object instance only when one object is referenced for the first time. Therefore, if you stop using this object in code, you will not create this object instance.
This may be useful if the Code only uses this object in some cases (may depend on the value of the request parameter. If this object is not required, the server resources can be saved.
However, you can use the server. Createobject method to create an object. Using the <Object> element to create an object helps prevent you from forgetting to cancel the server. Createobject line in the program when calling the object in the Code. Of course, this is a careless program design.
Remember that if the object is created using the server. Createobject method, you can remove the object from the session or application, but it cannot be created using the <Object> element.

5.2.4 component thread model
When using an object or component on a page, another issue that should be considered is the behavior of the object to respond to multiple requests. In fact, in ASP, this is one of the most complex questions to be understood. The thread model of a component, combined with its scope, affects the performance and efficiency of the component and application, and also affects the ASP page that instantiates it.
A thread is a system object executed by a processor to complete tasks defined by component code. Each thread can be considered a single binary instruction set. In a multi-threaded environment like Windows, multiple threads can run simultaneously.
There are actually five thread models (including the neutral-threading model introduced in Windows 2000 ):
? Single-threaded (single thread): Only one process can use a certain component at a time.
? Apartment-threaded (Unit thread): a certain component can be used by several processes, but only one is on the specified thread.
? Neutral-threaded (neutral thread): a number of processes can use a certain component and can use any of the specified threads.
? Multiple-threaded or free-threaded (multi-thread or free thread): a number of processes can use a certain component, and these processes can run on different threads.
? Both-threaded (double thread): the object can be a unit thread and can be used as a free thread.
Here we will not explain the technical details of the thread model. This book will be followed by relevant content.
The component of the unit thread (for example, a component created using Visual Basic or used as an XML script) can run well in the page layer scope and is acceptable in the Session Layer scope. In fact, at the page layer, dual-threaded components can be well run due to low data processing overhead.
The neutral thread model in winodws 2000 even provides better performance, although so far there are only a few such components and development tools suitable for it.
If you need a Session Layer component, use the available dual-threaded components. If the application layer scope is required, the dual-threaded components can be used all the time.
However, Microsoft recommends that you do not use components at the Session Layer scope, or even components at the application layer scope, unless these components are absolutely required. Making the activity time of a component exceeds the time required by a page-level component does not benefit objects that provide Proxy features by COM +.

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.