ASP 3.0 Advanced Programming (13)

Source: Internet
Author: User
Tags format contains copy error code execution reference string access
Programming | Advanced 1.3 ASP Server objects
As seen earlier, there are quite a few things you can do with traditional dynamic page directives and commands through the server-side pages that are included with ISAPI to access the Web server. But at the same time there are some obvious limitations.
For example, you can retrieve the value of the HTTP header that was sent over from the Request.ServerVariables collection to all the requests that accompanied the client. It is almost as #echo匹敌 as using SSI, with the primary advantage of returning these values as strings to the code (while the #echo instruction simply inserts the values into the page), so that they can be retrieved and maintained according to their desires. Many of the same parameters apply to #fsize and #flastmod directives, and you can easily get this information using scripts with objects in VBScript and the JScript script engine. In the following chapters you will see the relevant details.
#exec指令既非常有用, and subject to certain restrictions. In fact, the directive runs only system commands or custom CGI applications and does not provide a true control of the process to the script. ASP server objects provide an entirely new way to be more secure and easier to run other applications or components than #exec directives. Of course, for some cases, especially where you really need to execute an operating system command or an existing CGI application, #exec是无法替代的.
To study the server object, first outline all of its available methods and properties, and then discuss it in more detail.

4.3.1 ASP Server Object Members overview
The server object is designed specifically to handle specific tasks on the server, especially those related to the server's environment and processing activities. So there is only one property that provides information, but there are seven ways to format data in a server-specific way, manage the execution of other Web pages, manage the execution of external objects and components, and handle errors.
1. Properties of the server object
The only property of the server object is used to access the script timeout value for an executing ASP page, as shown in table 4-2:
Table 4-2 Properties and descriptions of server objects
Characteristics
Description

ScriptTimeout
Integral type. The default value is 90.
Sets or returns the number of seconds a script can execute before the server exits execution and reports an error. When this value is reached, the page is automatically stopped and the pages that contain errors that may go into the dead loop are deleted from memory or pages that wait for other resources for a long time. This prevents the server from overloading with the wrong page. You need to increase this value for a page that is running for a long time

2. Method of the Server object
The method of the server object is used to format data, manage Web page execution, and create other object instances, as shown in table 4-3.
Table 4-3 Server object method and description
Method
Description

CreateObject ("identifier")
Creates an instance of an object (a component, application, or Script object) identified by identifier that returns a reference that can be used in code. Can be used for a virtual application (Global.asa page) to create a session-level or application-layer-wide object. The object can be identified with its classid, such as "{clsid:bd96c556-65a3 ... 37A9} "or a ProgID string to identify, such as" ADODB. Connection "

Execute ("url")
Stops execution of the current page and transfers control to the Web page specified in the URL. The user's current environment, which is the session state and current transaction state, is also passed to the new Web page. After the page executes, control is passed back to the original page and the statement following the Execute method continues

GetLastError ()
Returns a reference to an ASP ASPError object that contains the detailed data for the most recent error that occurred during ASP processing of the page. The information given by the ASPError object contains the file name, line number, error code, and so on

HTMLEncode ("string")
Returns a string that is a copy of the input value string, but removes all illegal HTML characters, such as <, >, &, and double quotes, and converts to an equivalent HTML entry, that is, <, ' > ', ' & ', ' ' etc.

MapPath ("url")
Returns the full physical path and file name of the file or resource specified in the URL

Transfer ("url")
Stops execution of the current page and transfers control to the page specified in the URL. The user's current environment, which is the session state and current transaction state, is also passed to the new page. Unlike the Execute method, when a new page executes, it does not return to the original page, but ends the execution process

UrlEncode ("string")
Returns a string that is a copy of the input value string, but all characters that are not valid in the URL, such as?, &, and spaces, are converted to equivalent URL entries, namely%3f,%26, and +


4.3.2 Create an instance of another object
In the previous chapter, we discussed ASP's virtual application concepts, and learned that virtual applications provide process isolation through application protection settings for components and other objects in ASP Web pages. This continues the 1th chapter of the discussion of how ASP's ObjectContext objects provide a running environment for ASP Web pages, and how to use other components and objects that run in the same environment.
ASP server objects provide the ability to create these components and application instances, and therefore can be used to augment ASP scripts. This functionality is achieved by implementing a specific version of the CreateObject method.
1. Creating object instances in VBScript and JScript
In VB or VBA, you can use several methods to create an instance of an object. You can create a new object of the specified type by using the New keyword:
Dim Objnewobject as New mycomponent
However, you cannot do this with VBScript or JScript in ASP, because these scripting engines do not implement data type definitions. You cannot declare a variable to be any specified data type, its variables are variants types, or an equivalent type (depending on the scripting language used).
Another method in VB and VBA is to use the CreateObject or GetObject method. The parameter of the CreateObject method is a classid (usually) or a ProgID string that returns a new object of the corresponding type:
Set Objnewobject = CreateObject ("ADODB. Connection ")
When you have a specified document type and you want to create an object instance that can work with such a document, you typically use the GetObject method:
Set objexcel = GetObject ("C:\MYFILES\SALES.XLW")
You can also specify the type of object and file name you want, which is useful in situations where several objects can handle the document type:
Set objexcel = GetObject ("C:\myfiles\sales.xlw", "Excel.Application")
VBScript supports CreateObject and GetObject methods. JScript also has a GetObject method that works the same way as GetObject in VBScript. The ActiveXObject in JScript implements the same functionality as VBScript's CreateObject method. However, this function is often used in conjunction with the new operator of JScript:
Objnewobject = new ActiveXObject ("This.object");
All of these techniques can be used to create an instance of an object in an ASP Web page, except that the new keyword in VB is not supported in VBScript and JScript. However, being able to do so does not mean that it should, and in most cases should not use the scripting engine's object creation function in an ASP Web page.
2. To create an object instance in an ASP Web page
In order to understand why the generic scripting engine object creation method does not work well in ASP Web pages, it is necessary to further discuss the environment and ObjectContext objects in ASP.
The generic method of using the scripting engine when you create an object instance in an ASP Web page, the object is not instantiated in the context of the currently executing page. Cannot get a reference to a ObjectContext object, you cannot use it to access the environment of the page, that is, you cannot access values in the page environment.
This means that the object cannot use a built-in ASP object, that is, not having access to values in the collection of request, Response, application, and session objects, or the methods and properties provided by the built-in ASP object. The object is also not able to interact with any existing transactions in this environment. If an error occurs, you cannot discard a transaction using the ObjectContext method.
Of course, you may not want to interact with the environment of the Web page. But there are other reasons why it is often unwise to use generic object-creation methods. IIS automatically instantiates objects in the COM + run-time wrapper Hllhost.dll so that the object can be completely shared and reused in the current virtual application (the default Web site itself is a virtual application).
You



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.