Language Features of ASP (Active Server Page)

Source: Internet
Author: User
Language Features of ASP (Active Server Page)
The Web is derived from static text, which is almost used by many websites. The disadvantage of static text is that it is difficult to maintain (the number of files is large), difficult to query, and difficult to modify. Although FrontPage can be used for site management, this is only one method. Currently, many websites use databases in the background to dynamically generate hypertext. In this way, to change the style, you only need to modify a few files. To change the content, you only need to perform operations on the database, which is easy to maintain. For query applications such as full-text search, the superiority of this method is even more prominent. (Often referred to as "dynamic" also includes dynamic display of images, such as dynamic GIF and Java Applet. This article describes the dynamics of materials.) General Gateway Interface CGI (Common Gateway Interface) and the emergence of Internet Server Application Programming Interface (ISAPI) and other related technologies, so that we can easily communicate with the Web server, as a result, CGI programs such as counters and message boards have become increasingly popular. However, these CGI programs have a major drawback: CGI (which can be written in VB, Java, Perl, C, or dephi) and HTML can only be compiled and run separately, it is not easy to integrate the two, which makes maintenance and writing quite difficult. As a result, some vendors later launched their own script language to Enhance web development features. Among them, Netscape introduces JavaScript (for client and for server); Microsoft introduces VBScript and ASP (ASP syntax is similar to VB syntax, it is also used for compiling server and client programs. For professional developers, the most important aspect of client programs is universality, while the server end emphasizes functions and efficiency. For client-side program development, JavaScript is undoubtedly the best, because today's two mainstream Browser IE and Netscape both claim to support this standard, while VBScript is only supported by IE. But what worries us is that some JavaScript markup, some versions of IE or IE may not be supported, which will make us have to move some programs to the server for execution, because the server-side program will first interpret and generate HTML (or ActiveX) text and then send it to the client, there is no compatibility problem.

Livewire (JavaScript For server compiler) is the same as ASP, all are some manuscript languages (a manuscript refers to a program segment that is similar to the JavaScript code on the client end and can be embedded in HTML text, and can be marked as executed by the server only ), that is, an object-oriented language similar to C/C ++. Livewire file suffix is HTML, ASP file suffix is ASP, because ASP files are explained by the server only when the client reads them, livewire files must be pre-compiled (re-compiled after each modification ). ASP has an auxiliary development tool (InterDev) to generate some controls. Of course, these are not important. The key is whether you use Netscape Web server or Microsoft IIS. But in general, because IIS is bound to NT, there will be more users using IIS.

Since Netscape launched Livewire and Microsoft launched Active Server Page, it is much easier for programmers to write online programs. However, this script language has a common drawback, that is, because it is interpreted and executed, the speed will not be too fast. Recently, Microsoft recommended a three-tier model. The middle layer can directly use ActiveX controls generated by VB6.0 without ASP. This is faster because it is compiled and executed.

  

1. Basic ASP object

  

ASP (Active Server Page) is an object-oriented programming language similar to VB. Here, we do not want to detail some basic concepts such as objects, but directly introduce their language features, so that developers can skillfully modify and compile ASP programs (because many developers simply use development tools such as InterDev to generate ASP programs ).

First, the archive name of the ASP program must be "asp". Second, in the ASP program, the script on the server must be placed in the middle of the <% and %> string, for example, <% USER = request ("user") %>. If you want to call another ASP file in an ASP file, you can add the following statement in ASP:

<! -- # Include virtual | file = "***. asp" -->, which is similar to # include in C. Virtual indicates the virtual file path, and file indicates the real file path.

ASP has five main built-in objects: request, response, server, session, and application. The following describes them as an example.

  

1.1 request

ASP still follows the popular method of using the form on the client for data exchange. The ASP built-in request object has five methods to obtain client data (querystring/form/cookies/servervariables/clientcertificate ).

Syntax: request. Method Name ("parameter") | request ("parameter ")

If the latter is used, the system automatically selects the corresponding method.

1.1.1 form

Here we will re-emphasize some attributes of form:

<Form action = the URL of the called Program

Enctype = mime form of Data Transmission

Method = method of data transmission get/post

Onrest = program called by pressing the rest key

Onsubmit = press the sumit key to call the program

Target = frame window of the output content> </form>

There are three methods to transmit information by using form: transmitting information from form on the HTML webpage to other ASP; transmitting information from form in ASP to another ASP; information in ASP is transmitted to itself.

Syntax: request. Form (parameter) [(INDEX)]

The parameter indicates the name of the element in form, and the index indicates the sequence number of the element with the same name.

Example:

<%

For I = 1 to request. Form ("user"). Count

Response. Write Request. Form ("user") (I)

Next

%>

Users can be read cyclically. Count is the system attribute of form and is used to calculate the number of elements with the same name. If the element does not exist, the value is zero. If it does not specify the number of elements with the same name to be read, the system reads the values of all elements with the same name and uses "," as the interval.

Example: request. Form ("user") = ABC, BCD, CDE

  

1.1.2 querystring

Syntax: request. querystring (parameter) [(INDEX)]

In addition to using form to transmit data, you can also connect "?" For example, <a href = "***. asp? User = ABC ">, you can use the request. querystring ("user") reads the transmitted information. If multiple parameters with the same name exist, for example, <a href = "***. asp? User = ABC & User = BCD & User = CDE ">, the first resquest. querystring ("user") = ABC, the second resquest. querystring ("user") = BCD, the third resquest. querystring ("user") = CDE.

  

1.1.3 servervariables

Syntax: servervariables (parameter name)

We know that the Web/browse transmission protocol is HTTP, And the HTTP header has some client information, such

Customer IP address, browser language system, etc. In this case, you can obtain

Obtain the relevant information, such as request. servervariables ("accept_language ").

. Other system parameters are shown in the following table:

The name or IP address of the server.

The Port Number of the server running.

Request Method (get/post/head ).

The path to which the script_name program is called, for example, cgi-bin/a. pl.

The name of the remote machine (client) That remote_host sends the request.

The IP address of the remote machine (client) That remote_addr sends the request.

The name of the user whose remote_ident sends the request (if the request is dial-up, it is the user ID ),

When NCSA identitycheck is enabled, and the client machine supports

This variable is valid when RFC 931 is used.

Mime Type of content_type data, for example, "text/html ".

List of MIME types acceptable to http_accept client.

The type of browser in which http_user_agent client sends a request.

Http_referer refers to the text URL of the client before reading the CGI program.

  

1.1.4 cookies

On the client side, cookies record a lot of information about the client browser. the cookie ("name") command can also be used to obtain its value through response. cookies ("name") = "value" records some information on the client to control visitors. The method for setting multiple cookies is response. Cookies ("name") ("name") = "value ".

  

1.1.5 Cache

We can select the page information that has been accessed from the cache in the browser. Similarly, we can perform similar settings in the ASP program. Response. Clear indicates clearing the client memory. Response. Buffer = true set to read data from the cache (false by default ).

  

1.1.6 clientcertificate

Clientcertificate is used to obtain the Identity confirmation information of the client browser (conforming to the X.509 standard),

The client browser must support SSL3.0 or pct1. Here we need to do two steps. Step 1: The Web server must start the client authentication option; Step 2: Set the client browser so that this method will take effect. Otherwise, returns the empty value.

  

1.2 response

To transfer data from the server to the client, use the following method:

1) response. Write directly outputs information, such as response. Write "<HTML> ".

2) response. redirect directs the client to another URL location, such as response. redirect "http://zyr.yeah.net", but here there is a problem to note that the statement must be executed before the <HTML> mark, that is to say, the server will be executed before the server sends an HTML text header to the client. Otherwise, a transmission error will occur.

3) response. contenttype controls the output file type. The data packet type sent from the server to the client can be text/HTML or GIF/JPEG. Therefore, before each transmission, we must all inform the client of the file type to be transmitted, which is generally "text/html" by default. For example, <% response. contenttype = "image/JPEG" %>.

4) response. Cookies are used to set the cookie value. Syntax: Response. Cookies (variable name) [(key) | attribute] = written information. (Key usage is described earlier. This is not described here) the cookies system has five default attributes, including expires (time), domain (domain name), path, and secure (Security) and haskeys (determine whether there are other cookies under cookies ). For example, <% response. Cookies ("type"). expires = "Sep 9,1998" %>.

5) response. buffer is used to determine whether information is transmitted to the buffer. Because sometimes we want to send some intermediate information to the buffer and continue to work. After all the processing work is completed, the information is output to the client. However, when the flush or end method is called during processing, the buffer content is output.

Other response attributes:

Expires: Set the length of time for retaining the web page in the client browser.

Expiresabsolute: set the date and time when the webpage is retained in the client browser.

Other response methods:

Addheader: sets the HTTP header of an HTML file.

Appendtolog: Add a string at the end of the Web server record file.

Clear: Clear HTML output data in the buffer.

End: stop processing ASP files and return the current status.

Flush: sends the HTML data of the buffer immediately.

  

1.3 Server

The server object provides you with fixed server methods and attributes, including scripttimeout, mappath, and urlencode. Scripttimeout is the attribute of server, which specifies the maximum execution time of an ASP program, for example, server. scripttimeout = 200 (the counting unit is seconds, and the default value is 90 seconds ).

Mappath is a server method used to convert the web server's virtual path to the actual path. For example, server. mappath ("/") = C:/inetpub/wwwroot. asp treats path parameters "/" and "/" as the same. Note that the system only outputs results in the real root directory of the web server, instead of checking whether such a directory exists in the file system.

Urlencode is another method of server. This method will compile the source string according to the URL encoding principle.

The Code is the target string, and the blank characters are replaced by "+.

  

1.4 session

Session is a very important object in ASP. When programming in some traditional languages, the question of whether a variable is defined as a global variable or a local variable is involved. However, this issue is rarely mentioned in Internet Development (except Java) because every HTML page is irrelevant and all links are disconnected after transmission. In ASP, we can define some global variables. The global variables here mean they can be used before the current browser is disabled. These variables can be defined in global. Asa. If your web application is automatically created using InterDev, the file is located in the root directory. If not, you can create the file by yourself.

Syntax: Session. Attribute | Method

Generally, the set keyword is used to create a new session object, for example, <% set session ("variable name") = value to be assigned. If an array is assigned to the session variable, it must be stored in the array during reading, because the system regards it as an object ).

  

1.5 Application

Global. ASA is a file used to define session, application, and object objects. The client cannot see this file. However, the file must be placed in the root directory of the program, and the text cannot contain HTML tags and the file name cannot be changed. The following is an example of the global. Asa text.

<Script language = VBScript runat = Server>

Sub session_onstart

'= Visual InterDev generated-dataconnection startspan =

'-- Project data connection

Session ("nnb_connectionstring") = "DSN = ***; uid = zyr; APP = Microsoft (r) Developer

Studio; wsid = zyr; database = ***"

Session ("nnb_connectiontimeout") = 15

Session ("nnb_commandtimeout") = 30

Session ("nnb_runtimeusername") = "zyr"

Session ("nnb_runtimepassword") = "******"

'= Visual InterDev generated-dataconnection endspan =

End sub

</SCRIPT>

The difference between application and session is that online users can use application objects, so they need to lock them. Session is only for a single customer. For example, define an application variable as application ("name") = "1234 ".

What is asp? Of course not. More advanced ASP applications include ActiveX controls and other functions that can be called. To obtain more information, visit the following website:

Http://www.microsoft.com/asp

Http://zyr.yeah.net

Http://www.kj.com.tw

  

2. programming example of ASP

In order to give you a perceptual knowledge of the functions mentioned above, we will use a very simple chatroom (conversation room) Written in ASP as an example. To simplify it as much as possible, we only use four files, one of which is the main file main. ASP and enter. ASP (for the user to enter the user name), one is the data input file input. ASP, one is to display the file show. ASP. The principle is to define an application variable show. Every time someone talks, we add it to the front. In addition, the show file is displayed. ASP refreshes every three seconds to display the show variable. all work is just that. using other languages or methods may require more complex work. Here it becomes very simple and clear. (The Global. Asa file is not used here .)

  

2.1 main. asp file

<%

If SESSION ("sname") = "" then

Response. Redirect "enter. asp"

End if

If the user does not enter a name, the user will return to the name input page.

%>

  

<HTML>

<Frameset rows = "80%, 20%">

<Frame name = "show" src = "show. asp">

<Frame name = "input" src = "input. asp">

<Noframes>

<Body>

<P> This page uses frames, but your browser doesn't support them. </P>

</Body>

</Noframes>

</Frameset>

</Html>

  

2.2 enter. asp file

<%

Nickname = request. Form ("nickname ")

; Read user input name

If nickname = "" then

MSG = MSG + "<font color = 'ff000000'> enter your name </font> <br>"

Else

Response. Cookies ("whoami") = nickname

Write the username to cookies.

Application. Lock

Application ("show") = "<Table border = 0 bgcolor = 'ccffcc' width = '000000'> "_

& "<TD> <font color = '5555ff'> <B> to" & request. servervariables ("remote_addr ")_

& "& Request. Cookies (" whoami ") &"... </B> </font> </TD> </table> "_

& Application ("show ")

; _ Is a separator, the same as VB

Application. Unlock

Session ("sname") = nickname

Response. Redirect "Main. asp"

End if

%>

<HTML>

<Body>

<Form method = "Post" Action = "enter. asp">

Your name:

<Input type = "text" name = "nickname" size = "20" maxlength = "10">

<Input type = "Submit" value = "enter chat room" name = "B1">

</Form>

</Body>

</Html>

  

2.3 show. asp file

<HTML>

<Head>

<Meta http-equiv = "refresh" content = "3">

; Set to refresh every three seconds

<Meta name = "Microsoft border" content = "NONE">

<Title> chat content </title>

</Head>

<Body bgcolor = "# ffffff">

<% = Application ("show") %>

</Body>

</Html>

  

2.4 input. asp file

<%

Mytalk = trim (request. Form ("txtbox "))

Read user-written information. trim is used to remove spaces.

Application. Lock

Application ("show") = "<font color = ff0000> <B>" & request. Cookies ("whoami ")&_

"</B> </font>:" & mytalk & "<br>" & Application ("show ")

; Add the new information to the front of the application variable show, where request. Cookies ("whoami") are

The user's name is read in cookies.

Application. Unlock

%>

<HTML>

<Head>

<Meta name = "Microsoft border" content = "NONE">

<Base target = "input">

<Title> input page </title>

</Head>

  

<Body bgcolor = "# ffffff">

<Form method = "Post" Action = "input. asp" target = "input">

<P> <font color = "red"> <% = request. Cookies ("whoami") %> </font>

Input: <input type = "text" name = "txtbox" size = "30">

<Input type = "Submit" value = "send" name = "go">

</Form>

</Body>

</Html>

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.