ASP object structure
We have learned the basic application of ASP before. Now we need to systematically understand the object structure of ASP. Then, we can draw a line from each other without having to explain every example too much. This part may be difficult. However, you cannot be afraid to learn things, right?
Request object
Response object
Application Object
Session Object
Server Object
I. Request
The request object stores customer information in several collections for ASP applications. The common access method is:
Request. Collection ("membername ")
When you do not specify a set name, use (1) querystring, (2) form, (3) Cookie and (4) servervariable
Search all sets in sequence. When the first matched variable is found, it is determined that it is a member to be referenced. Of course, to improve efficiency, you 'd better explicitly specify a member in that set.
Querystring set
When HTML forms use the get method to transmit data to ASP files, the data is stored in the querystring collection. The member can have multiple values related to it. That is to say, in the same form, multiple elements can have the same name.CodeAccess the data:
<% For each item in request. querystring ("name ")
Response. Write item & "<br>"
Next %>
Form set
When the form uses the POST method, the data is saved in the form set.
Servervariable set
The information of the HTTP header sent along with the HTTP request is saved. You can use the header to obtain information about the browser. The main members include:
Remote_addr Remote Host IP Address
Remote_host remote host name
Remote_user CUSTOMER NAME
Request_method request method (such as post, get, head)
SERVER_NAME server name
Server_protocol server version (for example, HTTP/1. 0)
Ii. Response object
It is used to control the HTML content returned to the customer. There are several attributes and methods. The following describes what I think is important:
Buffer attribute
If the value is true, the content of response needs to be written to the buffer, and then sent to the customer after the script is processed.
Status attribute
The status of the HTTP Response Message. The status code returned by the server is composed of three digits and can be used for testing and conversion control to other sites (that is, forward)
Write Method
Output HTML to the customer, which can be any legal HTML Script.
Redirect Method
Redirect the browser to another URL, for example:
<% Browsetype = request. servervariables ("http_user_agent ")
If left (browsetype, 11) = "Mozilla/2.0" then
Response. Redirect "fancystart. asp"
Else
Response. Redirect "oldstart. asp"
End if %>
Clear Method
If the buffer attribute is set to true, the clear method is clear about all the buffer content.
Flush Method
Send the buffered content to the customer immediately.
End Method
When the Active Server encounters this method, it immediately stops processing ASP files. If there is a buffer, it immediately sends the content to the customer.
Binarywrite Method
Output binary data
3. Cookie set of the request object and response object
1. Write cookies
Response. Cookies ("Cookie name") [("key name"). Attribute] = Value
If the cookie already exists, the value is replaced by a new value. Otherwise, the cookie is created.
For example:
<% Response. Cookies ("newcookie") = "new cookie value" %>
2. read cookies
For example:
<% = Request. Cookies ("newcookie") %>
Cookies also have some attributes. For more information, see.
4. Application Object
Active Server ApplicationProgramIs all files in the virtual directory and Its subdirectories, that is, a web. The application object can be used to share information among all users of the application software, and data can be permanently stored during server running. He has some methods and events that control access to the application layer data.
The application itself has no built-in attributes and can be defined by the user:
Application ("property name") = Value
Data stored in the Application object can be read by all users of the application. For example:
Application ("avisits") = Application ("avisits") + 1
There are two methods:
Lock:
When a user calls lock, only the current user can edit or add attributes of the Application object.
Unlock:
Remember that when the lock is called, The unlock must be called.
There are also two events:
Application_onstart event: called when the application starts.
Application_onstart event: called when the application is terminated.
The two events, coupled with the session event handler, are stored in the file global. asp. A Web application has only one global. Asa file and is stored in the root directory of the application. An example of a global. asp file is as follows:
<Script language = "VBScript" runat = "server">
Sub application_onstart
Dim lachats (15)
Application ("gachats") = machats
Application ("gicounter") = 0
End sub
V. Session Object
Active Server uses session settings to persistently maintain data for a single user using the application. When you request the URL of ASP files in the active server application, you must start the session. By default, if there is no user request, the server only keeps session20 minutes. You can also change the value by setting the session attribute timeout. Or call the session. Abandon method explicitly to release the session object.
Sessionid attributes
The identifier that uniquely identifies a session.
Timeout attribute
Defines the time limit for session retention, in minutes, as shown in figure
Session. Timeout = 10
Like the application, the session can also be defined by the user.
The only method of session is abandon, which is used to cancel a user's session object and release the server resources it occupies. For example:
<% Session. Abandon %>
The events include session_onstart and session_onend. The processing program should be placed in the file glabal. Asa.
Vi. Server objects
1. htmlencode Method
Encode a specific string in HTML format. If you want to display the following content:
The underline tag (<u> </u>) is used to underline the surrounded text.
However, it is likely to be displayed as follows:
The underline tag () is used to underline the surrounded text.
To avoid this problem, you can call the htmlencode method of the server object, for example:
<%
Response. Write server. htmlencode ("the underline tag (<u> </u>) is used to underline the surrounded text .")
%>
2. urlencode Method
Encode the string according to the URL rules. When string data is transmitted to the server in URL format, there cannot be spaces or special characters in the middle of the string. In this case, you must use URL encoding.
3. Createobject Method
Used to create ActiveX component routines registered on the server machine. This is probably the most important method :-)
Syntax:
Server. Createobject ("componentname ")
The component that can be started as a routine can be all the built-in components that ActiveX can use, and actually any ActiveX component that exists on the server. For example, to use financial computing, follow these steps:
1. Create an object
<%
Set x = server. Createobject ("extend. Financial ");
%>
2. Call the object Method
<%
Set x = server. Createobject ("extend. Financial ")
Response. Write format (X. futval (. 07/12, 200,-500), "###,##,## 0.00 ")
%>
3. Release routine
<%
Set x = nothing
%>
VII. filesystem and textstream objects
Filesystem and textstream objects can be used to establish access to the file system and provide a mechanism for sequential file access. Filesystem has no attributes. There are only two methods. The first method is the createtextfile method. You can create a new text file on the host and return the textstream object to provide access to the newly created file. The second method is the opentextfile method, which is used to open text files for sequential access and return a textstream object. For example:
<%
Set fsfilesys = Createobject ("scripting. FileSystemObject ")
Set tscoffee = fsfilesys. createtextfile ("C: \ coffe.txt", true)
Tscoffee. writeline ("man, I cocould use some coffee .")
Tscoffee. Close
%>
Textstream objects are used as follows:
Atendofline: returns true if the current character of the file is at the end of the row.
Atendofscreen: returns true if the current character is at the end of the file.
Column: returns the column number of the current character.
Line: returns the row number of the current character.
The textstream object method is as follows:
Close: Close and release the textstream object.
Read: reads a given number of characters from a file to a variable.
Readall: reads all the content of a file to a variable.
Readline: Read the content of a given row number to a variable.
SKIP: Skips a specified number of characters.
Skipline: Skip the row book to the top
Write: Write as a string
Writeline: Write a string that ends with a line break.
Writeblanklines: number of spaces written to the top