Starting from this article, the author starts with ASP built-in objects and analyzes the features and methods of the six built-in objects and various components of ASP for you in detail.
Before officially starting to learn the built-in objects and components of ASP, let us first understand some basic concepts, which will be of great help to your future learning. Please see the table below:
Active Server A collection of server-side technologies delivered with Windows NT. These technologies provide consistent server-side components, scripting models and an integrated system service for component application management, database access, transactions, and messages.
Active Server Pages (ASP) A server-side scripting environment that runs ActiveX scripts and ActiveX components on the server. Developers can combine scripts and components together to create web-based applications.
ActiveX allows developers to create interactive components for the World Wide Web with rich Microsoft technical terms. A set of language-independent interoperability technologies that allow software components written in different languages to work together in a network environment. The key elements of ActiveX are the Component Object Model (COM) and the Distributed Component Object Model (DCOM). These technologies have been licensed by open organizations and have been ported to many platforms.
ADO Active Data Object. A set of object-based data access interfaces optimized for Internet-based data-centric applications. ADO is based on published specifications and comes with Microsoft Internet Information Server and Microsoft Visual InterDev.
DSN Data source name. An open database interface (ODBC) is used to refer to the logical name of the driver or other information required to access data. The name of the Internet Information Server that is used to connect to an ODBC data source, such as a SQL Server database.
event: Any action that the event is responded to by a program generated by a user or ActiveX control. General events include pressing the keyboard key, clicking the selection button by using the mouse, and other mouse operations. Programmers write code that responds to these operations.
object: An object is a variable composed of operations and data that are regarded as complete entities in object-oriented programming. Objects are based on a specific model in which clients use the object's services to access the object's data through an interface of a set of methods or related functions. The client can then call these methods to perform some operation.
ODBC Open Database Interface. Application programming interface that allows applications to access data from existing data sources standard specifications for various cross-platform data access.
SQL structured query language Structured Query Language. International standards for defining and accessing relational databases.
What is an object? It does not refer to the male or female companion you are in love. In object-oriented programming, an object refers to a variable composed of operations and data that are regarded as complete entities. Objects are based on a specific model in which the client uses the object's service to access the object's data through an interface of a set of methods or related functions, and the client can then call these methods to perform some operation. ActiveX components are key to building web applications, and components provide objects that perform tasks in scripts. An ActiveX component is a file that contains code that executes a certain item or set of tasks. Since the component can perform common tasks, programmers do not have to create code that performs these tasks themselves. Components can be leveraged as basic building blocks for scripts and web-based applications. As long as you know how to access objects provided by a component, even a newbie in scripting can write scripts without understanding how components work. In short, components allow you to write powerful scripts without learning programming. Components are executable code contained in the dynamic link library .dll or executable file .exe. Components may provide one or more objects as well as methods and properties of objects. To use the object provided by the component, create an instance of the object and assign the new instance the variable name. Use the Server.CreateObject method of ASP to create an instance of an object, and use the variable assignment directive of the scripting language to name the object instance. As shown in the following example:
Set db=Server.CreateObject(ADODB.Connection)
The variable db here is an instance of the object that accesses the database created by the ASP program.
Active Server Pages provides built-in objects that can be used in scripts. These objects make it easier for users to collect information sent through browser requests, respond to browsers, and store user information, thus freeing object developers from a lot of tedious work. The current ASP version provides a total of six built-in objects. Let's learn them through examples below.
1. Request object
You can use the Request object to access all information passed on any HTTP request, including parameters, cookies, and user authentication passed from HTML tables using the POST method or GET method. The Request object enables you to access binary data sent by the client to the server.
Request syntax:
Request[. Collection | Attribute | Method](Variable)
Here the author will select some commonly used object grammars for analysis
1. Form
The Form collection retrieves the values of the table elements sent to the HTTP request body by using the POST method.
grammar
Request.Form(element)[(index)|.Count]
parameter
element Specifies the name of the table element to retrieve the collection.
index Optional parameter, which allows you to access one of multiple values in a parameter. It can be any integer between 1 and Request.Form(parameter).Count.
Count Number of elements in the collection
The Form collection is indexed by the name of the parameter in the request body. The value of Request.Form(element) is an array of all element values in the request body. Determine the number of values in the parameter by calling Request.Form(element).Count. If the parameter does not associate multiple values, the count is 1. If the parameter is not found, the count is 0. To reference a single value in a table element with multiple values, the index value must be specified. The index parameter can be any number from 1 to Request.Form(element).Count. If one of multiple table parameters is referenced and the index value is not specified, the returned data will be a comma-separated string.
You can use the restater to display all data values in a table request.
For the hobby parameter, you can retrieve these values using the script below. < html> < head>< title>< /title>< /head> < body> < p> Please fill in your hobbies< /p> < form method=POST action=form.asp> < p>< input type= text name=hobby size=20>< br> < input type=checkbox name=hobby value= football> Football< input type=checkbox name=hobby value= table tennis> Table Tennis< /p> < p>< input type= submit value= Send name=B1>< input type=reset value= Refill name=B2>< /p> < /form> < % For Each i In Request.Form(hobby) Response.Write i & < BR> Next %> < /body>< /html>
Scrap the above code into the notepad (note that you remove the spaces after <), save it as a form.asp file and run it. The request object can display elements one by one according to the different contents you fill in or select in the form.
Of course, using the For...Next loop can also generate the same output, as shown below:
< %
For i = 1 To Request.Form(hobby).Count
Response.Write Request.Form(hobby)(i) & < BR>Next
%>
2. QueryString
The QueryString collection retrieves the value of a variable in an HTTP query string. The HTTP query string is specified by the value after the question mark (?). like:
< A HREF= example.asp?string=this is a sample>string sample< /A>
Generates a variable name string with the value this is a sample. Query strings can also be generated by sending a table or by the user typing a query into the address box of his browser.
grammar
Request.QueryString(variable)[(index)|.Count]
The QueryString collection allows you to retrieve the QUERY_STRING variable by name. The value of Request.QueryString(parameter) is an array of values of all parameters that appear in QUERY_STRING. You can determine how many values the parameter has by calling Request.QueryString(parameter).Count.
We can also use QueryString to achieve the same functionality as the previous example. Just replace the request.form partially as follows:
< %
For Each i In Request.querystring(hobby)
Response.Write i & < BR>
Next
%>
3. Cookies
What is a cookie? Cookie is actually a tag. When you visit a WEB site that needs to uniquely identify your site, it will leave a tag on your hard drive. The next time you visit the same site, the site's page Will look for this mark. Each WEB site has its own tag, and the tagged content can be read at any time, but it can only be done by the pages of that site. Cookies for each site exist in different files in the same folder as cookies for all other sites (you can find them in the Cookies folder in the Windows directory). A cookie is a tag that uniquely identifies a customer. A cookie can contain information shared by all pages of a WEB site between a conversation period or several conversation periods. Using cookies can also exchange information between pages. The cookies set provided by Request allows users to retrieve the value of a cookie sent in an HTTP request. This feature is often used in ASP programs such as requiring authenticated customer passwords and electronic bulletin boards, WEB chat rooms, etc.
grammar
Request.Cookies(cookie)[(key)|.attribute]
parameter
cookie Specifies the cookie whose value you want to retrieve.
key optional parameter to retrieve the value of a subkeyword from the cookie dictionary.
attribe Specifies the cookie's own information. For example: HasKeys is read-only, specifying whether the cookie contains keywords.
Subkeywords of the cookie dictionary can be accessed by including a key value. If a key is not specified when accessing the cookie dictionary, all keywords are returned as a single query string. For example, if MyCookie has two keywords, First and Second, and no one of them is specified when calling Request.Cookies, the following string will be returned.
First=firstkeyvalue&Second=secondkeyvalue
If the client browser sends two cookies with the same name, the Request.Cookie returns the one with the deeper path structure. For example, if there are two cookies with the same name, but one of them has a path attribute of /www/ and the other is /www/home/, the client browser sends both cookies to the /www/home/ directory at the same time , then Request.Cookie will only return the second cookie.
To determine whether a cookie is a cookie dictionary (whether a cookie has keywords), use the following script.
< %= Request.Cookies(myCookie).HasKeys %>
If myCookie is a cookie dictionary, the preceding assignment is TRUE. Otherwise, it is FALSE. Let's take a look at an example of a cookie:
< %
nickname=request.form(nick)response.cookies(nick)=nickname
' Use the response object to write the user name into the cookie
response.write Welcome &request.cookies(nick)& to the site!
%>
< html>< head>< meta http-equiv=Content-Type content=text/html; charset=gb2312>
< title>cookie</title>
< meta name=GENERATOR content=Microsoft FrontPage 3.0></head>
<body>
< form method=POST action=cookie.asp>
< p>< input type=text name=nick size=20>
< input type=submit value= Send name=B1>< input type=reset value= Refill name=B2>< /p>< /form>
< /body>< /html>
This is actually a common method in WEB-based BBS or CHAT ASP programs. It saves the name that the user fills in on the starting page in a cookie, so that the subsequent program can easily call the user's nick Now.
4. ServerVariables
Everyone knows that the transmission protocol used when browsing web pages in a browser is HTTP. Some client information will be recorded in the HTTP title file, such as: the client's IP address, etc. Sometimes the server needs to be based on different client information. To make different responses, you need to use the ServerVariables collection to obtain the required information.
grammar
Request.ServerVariables (Server Environment Variable)
Due to the large number of server environment variables, the author only lists some commonly used variables in the following table:
| ALL_HTTP | All HTTP header files sent by the client. |
| CONTENT_LENGTH | The length of content issued by the client. |
| CONTENT_TYPE | The data type of content. For example: text/html. Used with additional information queries, such as HTTP queries GET, POST, and PUT. |
| LOCAL_ADDR | Returns the server address that accepted the request. This variable is very important if you look up the address used by the request on a multi-host machine that binds multiple IP addresses. |
| LOGON_USER | The user logs into the Windows NT account. |
| QUERY_STRING | Query the information after the question mark (?) in the HTTP request. |
| REMOTE_ADDR | The IP address of the remote host (client) that issued the request. |
| REMOTE_HOST | The name of the host (client) that issued the request. If the server does not have this information, it will be set to the empty MOTE_ADDR variable. |
| REQUEST_METHOD | This method is used to make a request. It is equivalent to GET, HEAD, POST, etc. used for HTTP. |
| SERVER_NAME | The server host name, DNS pseudonym, or IP address that appears in the self-referenced URL. |
| SERVER_PORT | The port number to send the request. |
We can print out all server environment variables using the following script.
< TABLE>
< TR>< TD>< B>Server Variable< /B>< /TD>< TD>< B>Value< /B>< /TD>< /TR>< % For Each name In Request.ServerVariables %>
< TR>< TD> < %= name %> < /TD>< TD> < %= Request.ServerVariables(name) %> < /TD>< /TR>< /TABLE>
< %Next %>
Today we have learned in detail the request object in ASP built-in objects, which is also the most frequently used object in ASP programs. I hope everyone will practice it after class.