Fully parse server objects

Source: Internet
Author: User

The server object provides methods and attributes for access to the server. Most methods and attributes are provided as functions of the utility.

Syntax:

Server. Property | Method

Property)

The server object has only one attribute: maximum time that the scripttimeout program can run

Method (methods)

Createobject creates an object instance.

Execute to execute an ASP file

Getlasterror returns an error code

Htmlencode converts the specified HTML code.

Mappath converts a relative path to an absolute path.

Transfer sends all current status information to another ASP file

Urlencode converts the specified code in URL format, including Spaces

Server Object Methods

Createobject

Syntax

Server. Createobject (progid)

Parameters

Progid

Specify the name of the component to be created. The format is as follows: [Vendor.] component [. version].

Key points:

Generally, an object created using the server. Createobject method has a page range. This means that this object will automatically disappear after the ASP program on this page is executed.

To create an object with a session or application range, you can use

Execute

The execute method calls an ASP file and executes it, just as the ASP file of this call exists in this ASP file. This is similar to calling classes in many languages.

Syntax

Server. Execute (PATH)

Parameters

Path

Specifies the path of the ASP file to be executed. If it is an absolute path, it must be a directory in the same place as the ASP application ).

Explanations

The server. Execute method provides a way to divide a complex ASP application into small units for execution. In this way, you can create an ASP library, and you can call ASP files in your library whenever you need them. This is a bit like ssi! Hey!

After IIS executes the ASP file based on the specified ASP file path, it will automatically return the previous ASP file. The newly executed ASP file may change the HTTP head. But like other ASP files, an error is reported when the program tries to change the HTTP head!

This path parameter can contain a query information.

If the ASP files to be called and called contain the same subfunctions, these subfunctions only play a role in this ASP file. For example, if the following asp1 and asp2 files contain child functions that discard the program. When asp1 calls asp2, ontransactionabort in asp2 starts to execute. When asp2 is executed, ontransactionabort in asp1 starts to execute.

Asp1:

<% @ Transaction = required %>

<%

Server. Execute ("page22.asp ")

Sub ontransactionabort

Sub ontransactioncommit

%>

Asp2.asp:

<% @

Transaction = required

Sub ontransactionabort

Sub ontransactioncommit

%>

Example

Asp1

<% Response. Write ("I am going to execute asp2 ")

Server. Execute ("/myasps/asp2.asp ")

%>

Asp2

<% Response. Write ("here I am") %>

Getlasterror

The getlasterror method returns an asperror object to describe an error message. This method is only applicable before the ASP file sends any content to the user.

Syntax

Server. getlasterror ()

Key Points

If a 500; 100 user error has been defined in an ASP application, it refers to a file suffixed with. asp. In this case, when an error occurs during the program running, the server will automatically send it to the ASP page that is being executed in the form of server. Transfer. The ASP application will effectively handle this error. In addition, this asperror object must be valid, so that you can see the error message provided by the server to change this file!

Generally, the web site is constructed based on the file \ IISHelp \ common \ 500-100.asp. You can use it to execute an ASP error. Of course you can define it yourself !. If you want to change to another ASP file to execute these user errors. Then you can use the snap-in.

Note: When IIS discovers an ASP file or an error in the global. Asa file, a 500 or 100 user error is generated. The following programs cannot be executed!

Example

The following three examples demonstrate the user errors caused by different errors. Three errors:

Compilation Error

Running Error

Logical error

The first example proves that a compilation error occurs when IIS tries to include a file. This error is generated because no required parameters are defined in this include file. The second example shows a running error. The program is interrupted because there is no "Next" in the program ". the third example shows a logic error because the program tries to divide it by 0. no!

Example 1

<%

Response. Write "hello"

%>

Example 2

<%

Dim I

For I = 1 to 1

NXT

%>

Example 3

<%

Dim I, j

Dim Sum

Sum = 0

J = 0

For I = 1 to 10

Sum = sum + 1

Next

Sum = sum/J

%>

Htmlencode

The htmlencode method is used to encode the specified string in HTML.

Syntax

Server. htmlencode (string)

Parameters

String character to be encoded

Example

The following program:

<% = Server. htmlencode ("the paragraph Tag:") %>

Output:

The paragraph Tag:

Note:

The paragraph Tag:

However, if you use "View Source File", the source code is no longer used.

Mappath

The mappath method converts the relative path to the physical path on the server.

Syntax

Server. mappath (PATH)

Parameters

Path

Relative Path. This path starts with "/" or "\". If the path does not contain "\", the mappath method returns the path based on the current directory.

Explanations

The mappath method cannot check whether the path exists under this server. This is because the mappath conversion path does not matter whether the path exists under the server or not.

You can use it to convert a relative path to a physical path, and then perform various operations under this path. {Shanghai impotence hospital}

Example

In the following example, the data.txt file exists in the C: \ Inetpub \ wwwroot \ Script directory, and a test. asp file contains the following code. C: \ Inetpub \ wwwroot is the main directory of the server.

In the following example, we first use the environment variable "path_info" to obtain the physical path of the current file.

The following is the script code:

<% = Server. mappath (request. servervariables ("path_info") %>

Shown:

C: \ Inetpub \ wwwroot \ Script \ test. asp

In the following example, the path parameter does not start with "/", so it is converted to the current directory. asp files are stored in c: \ Inetpub \ wwwroot \ script. the following is the content of scripts:

<% = Server. mappath ("data.txt") %>

<% = Server. mappath ("script/data.txt") %>

Shown:

C: \ Inetpub \ wwwroot \ Script \ data.txt

C: \ Inetpub \ wwwroot \ Script \ data.txt

The following two examples start with "/". The following are the content of scripts:

<% = Server. mappath ("\ script") %>

Shown:

C: \ Inetpub \ wwwroot \ Script \ data.txt

C: \ Inetpub \ wwwroot \ Script

Directly use "/" or "\" to obtain the server's main directory:

<% = Server. mappath ("\") %>

Shown:

C: \ Inetpub \ wwwroot

C: \ Inetpub \ wwwroot

Transfer

The transfer method transmits all information about an ASP file being executed to another ASP file.

Syntax

Server. Transfer (PATH)

Parameters

Path

Location of the ASP file to receive information.

Key Points

When you call server. Transfer, the status information of all built-in objects will be included in this transfer. This means that all information stored in the session or application will be transmitted, and all information of the current request will be accepted by the ASP file that receives the information.

Example

The following example demonstrates how to transfer an ASP file to another ASP file!

Asp1

<% Dim sessvar1 response. Write session. sessionid

Response. Write ("")

Response. Write ("I Am Going To asp2 ")

Server. Transfer ("/myasps/asp2.asp ")

%>

Asp2

<% Response. Write session. sessionid %>

Urlencode

The urlencode method can encode a specified string.

Syntax

Server. urlencode (string)

Parameters

String specifies the string to be converted

Example

The following code is used:

<% Respones. Write (server. urlencode ("http://www.microsoft.com") %>

Shown:

HTTP % 3A % 2f % 2 fwww % 2 emicrosoft % 2 ECOM

Attribute: scripttimeout

The scripttimeout attribute specifies the maximum running time of the program.

Syntax

Server. scripttimeout = numseconds

Parameters

Numseconds

Specifies the maximum running time (in seconds) of the program ). The default value is 90 seconds.

Remarks

The default scritpt timeout value can be set on Web sertvic or Web server through the aspscripttimeout attribute. In a program, the value of the scripttimeout attribute cannot be smaller than the default value. For example, if numseconds is set to 10 seconds and the default value is 90 seconds, the program will be suspended after 90 seconds instead of 10 seconds. Similarly, if we set the scripttimeout value to 100 seconds, the program will be suspended after 100 seconds, instead of 90 seconds.

Example

In the following example, the program will be automatically aborted after being set to 100 seconds.

<% Server. scripttimeout = 100%>

In the following example, we will obtain the value of scripttimeout again and store it in the timout variable.

<% Timeout = server. scripttimeout %>

Fully parse server objects

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.