2. Introduction to ASP

Source: Internet
Author: User
After reading the article "How to make your website" dynamic, do you feel at ease? Are you eager to build your own dynamic website? This article will take Active Server Pages as the center to show you the steps and skills for creating dynamic commercial websites and use a large number of examples, let you laugh at the Internet in constant theory and practice "......

The previous chapter briefly introduced some methods and tools for creating dynamic websites, this chapter will focus on how to establish an ASP Dynamic Website Based on IIS to reveal the real mysteries of dynamic commercial website design step by step. To enable you to master ASP development skills in an all-round and meticulous manner, this article will be serialized to teach you how to build your own ASP dynamic website. This article is written by the author based on his own learning and practical experience combined with some foreign materials. Therefore, I hope you will forgive me. If you find any mistakes in this article, please notify the author in time. Thank you.

Microsoft Active Server Pages is what we call ASP. It is actually a server-side scripting environment developed by Microsoft. asp is included in IIS 3.0 and 4.0, through ASP, we can combine HTML web pages, ASP commands, and ActiveX components to build dynamic, interactive, and efficient Web server applications. With ASP, you don't have to worry about whether your browser can run the code you have written, because all programs will be executed on the server side, including all script programs embedded in common HTML. After the program is executed, the server only returns the execution results to the client browser, which reduces the burden on the client browser and greatly improves the interaction speed. The following lists some unique features of Active Server Pages:
1. Using VBScript, JScript, and other easy-to-understand scripting languages, combined with HTML code, you can quickly complete website applications.
2. It is easy to compile without compile compilation and can be directly executed on the server side.
3. Use a common text editor, such as a Windows notepad, to edit the design.
4. browser independence. The user can view the webpage content designed by Active Server Pages by using a browser that can execute HTML code. The scripting languages (VBScript and JScript) used by Active Server Pages are all executed on the Web server. The user-side browsers do not need to be able to execute these scripting languages.
5. Active Server Pages can be compatible with any ActiveX Scripting Language. In addition to VBSCRIPT or JScript, you can also use other scripting languages provided by a third party, such as Rexx, Perl, and TCL, in Plug-In mode. The script engine is a COM (Component Object Model) object that processes the script program.
6. The source program of Active Server Pages will not be uploaded to the client's browser. Therefore, the source program can be copied by others and the program security is improved.
7. You can use the server scripts to generate client scripts.
8. object-oriented ).
9. ActiveX Server Components (ActiveX Server Component) is infinitely scalable. You can use Visual Basic, Java, Visual C ++, COBOL, and other programming languages to compile your required ActiveX Server component.
The wonders of ASP are really amazing. Please fasten your seat belt below. I will lead you into ASP's dream world.

First, let's look at the environment required to run ASP:

· Microsoft Internet Information Server version 3.0/4.0 on Windows NT Server
· Microsoft Peer Web Services version 3.0 on Windows NT Workstation
· Microsoft Personal Web server on Windows 95/98
As mentioned above, it is different from general programs ,. ASP programs do not need to be compiled. The control part of ASP programs is designed using VBScript, JScript, and other scripting languages. When ASP programs are executed, the script program sends a complete set of commands to the script interpreter (that is, the script engine), which translates them into commands that can be executed by the server. Of course, like other programming languages, ASP programming follows certain rules. If you want to write ASP programs in your favorite scripting language, then, you must have a script interpreter that can interpret the language on your server. When you install ASP, the system provides two scripting languages: vbsrcipt and JScript, while VBScript is used as the default scripting language of the system. You can also change the default scripting language of the system based on your preferences. For how to change the default scripting language of the system, see the article "using scripting language in ASP.

ASP is not a scripting language. It only provides an environment for script programs embedded in HTML pages to run. However, to learn ASP well, you must master its syntax and rules. Now let's get to know and learn about Active Server Pages step by step.

ASP is actually based on the extension. ASP exists in the form of plain text on the Web server. You can open it in any text editor. ASP programs can contain plain text, HTML tags, and script commands. You only need to place the. ASP program under the virtual directory of the Web server (the directory must have executable permissions), and then you can access the ASP program through www. To learn ASP program design well, you must master the writing of scripts. What is a script? In fact, scripts are composed of a series of script commands. For example, a program can assign a value to a variable, and the Web server can send a value to the client browser, you can also define a series of commands as a process. To write scripts, you must be familiar with at least one scripting language, such as VBScript. Scripting is a special language between HTML and programming languages such as Java, Visual Basic, and C ++. Although it is closer to the latter, however, it does not have complex and rigorous syntax and rules for programming languages. As mentioned above, the script running environment provided by ASP supports multiple scripting languages, such as JScript, Rexx, and Perl, which undoubtedly gives ASP programmers a wide range of opportunities to play. The emergence of ASP makes the majority of web designers do not have to worry about whether the customer's browser supports or not, in fact, even if you are in the same. you don't need to worry about using different scripting languages in ASP files, because everything will be done on the server side. What the client browser gets is the result of executing a program, and you only need. use different scripting languages in ASP. The following is a typical example of using two scripting languages in the same. asp file:
<HTML>
<Body>
<Table>
<% Call callme %>
</Table>
<% Call viewdate %>
</Body>
</Html>
<Script language = VBScript runat = Server>
Sub callme
Response. Write "<tr> <TD> call </TD> <TD> mE </TD> </tr>"
End sub
</SCRIPT>
<Script language = JScript runat = Server>
Function viewdate ()
{
VaR x
X = new date ()
Response. Write (X. tostring ())
}
</SCRIPT>

This is the first real ASP program that you have come into contact with in this article. Do not be confused by the "<%>" symbol. This is actually a standard ASP separator, the language between "<SCRIPT> </SCRIPT>" is the script language. Unlike the scripting language, Asp has its own specific syntax. All ASP commands must be included in <% and %>, for example, <% test = "English" %>, ASP outputs the execution result to the client browser by using expressions contained in <% and %>, such: <% = test %> is to send the English value assigned to the test variable to the client browser. When the value of the test variable is mathematics, the following program:
This weekend we will test <% = test %>.
In the customer's browser, It is shown:
This weekend we will test mathematics.

The best way to learn ASP is to write it in person. In order to help you master ASP programming skills in the shortest time, this article will adopt the example analysis method, through a series of instances, you can learn ASP in practice. To create an ASP page, you only need to open a text editor, such as Notepad, and then start to write the first ASP program with me. Next we will create an ASP program that automatically monitors the browsing time and dynamically displays the content of different pages according to different time periods. please cut the following code into your text editor and save it as test1.asp:
<HTML>
<Body>
<Font color = "green">
<% If time <#12:00:00 # And time >=# 00:00:00 # Then %>
Good morning. The weather is good today!
<% Elseif time <#19:00:00 # And time >=# 12:00:00 # Then %>
Good afternoon!
<% Else %>
Hello! Have you ever chatted with IRC tonight!
<% End if %>
</Body>
</Html>
Store test1.asp in a virtual directory (for example, aspsamp/) of the web server and use HTTP in the browser for browsing, such as http: // yourcomputername/aspsamp/test1.asp, you will find that your page is actually alive. Although this is just a very simple example, and this function can be completely completed through JavaScript, it is not difficult to find that using ASP is much simpler and faster than using JavaScript, and this method is also used, you can easily display different styles on your webpage at different times. In this example, "time" is actually a function built in VBScript that displays the current time of the system. Because the default script language of the system is VBScript, when you call this function in the ASP command, the script engine automatically converts it to the current system time. Next, we will add some color to test1.asp, add "bgcolor =" <% = BGC %> "to the <body> ID to <body bgcolor =" <% = BGC %> ">, add the following statement before <body> flag:
<% If time <#12:00:00 # And time> = #00:00:00 # Then
BGC = "Silver"
Elseif time <#19:00:00 # And time> = #12:00:00 # Then
BGC = "Navy"
Else
BGC = "red"
End if
%>

In this way, when users access your page at different times, they will see different page background colors. There are still many things we can do. For example, you want to know the name of the customer who browsed your page between AM and say hello to him or her, the following program will help you fulfill your wish. First, you need to set the form on the page and paste the following HTML code after "<% if time <#12:00:00 # And time >=# 00:00:00 # Then %>:
Welcome to my homepage. Enter the following information: <form method = "Post" Action = "test1.asp">
<P>
First name: <input name = "fname" size = "48">
<P>
Last name: <input name = "lname" size = "48">
<P>
Title: <input name = "title" type = radio value = "Mr"> mr.
<Input name = "title" type = radio value = "Ms"> MS.
<P> <input type = submit> <input type = reset>
</Form>

Add the following ASP command after the preceding HTML code: <%
Title = request. Form ("title ")
If title = "Mr" then
%>
Welcome to mr. <% = request. Form ("fname") %>.
<% Elseif Title = "Ms" then %>
Welcome to ms. <% = request. Form ("fname") %>.
<% Else %>
<B> <font color = blue> welcome <% = request. Form ("fname") & "" & request. Form ("lname") %>. </Font> </B>
<% End if %>

Save the test1.asp file and browse it via HTTP in the browser. If the system time is between 0:00:00 a.m. And 12:00:00 a.m., the browser will display the following screen:

This is actually a common function on the Internet and Intranet, that is, when the user fills out the form in the browser, by calling a general Gateway Program to transmit the number of users to the server, after being processed by the server, the result is returned to the client's browser. In the past, in order to implement such a function, we had to write a CGI program independent of HTML and call it through HTML. Apart from the disadvantages of complicated CGI writing, the execution efficiency of CGI is also a big problem, each form must execute an executable document. When multiple users go online at the same time, multiple documents are executed at the same time, which greatly reduces the Web server execution speed, now ASP provides a fully integrated HTML programming environment, which is much easier than CGI.
At the end of this issue, let's take a look at ASP's loop function. For example, if you want to display 6 smiling faces in the center of the page to greet your page from 0:00:00 AM to AM, then you just need to cut the following command to "Good evening! ": <% For I = 1 to 6%>
<P> <center> </P>
<% Next %>

This is the most basic loop statement. It repeatedly calls a smiley image six times and displays it on the page. Of course, the effect of this example can be fully achieved using HTML, but it is not difficult to find that the use of ASP greatly shortens the code re-writing, so that the program has a good readability. In addition, when you create a rating site, such as rating the star rating for the evaluated object based on the user's vote, you do not need to create an image for each star rating. If an object is rated as 4 stars, you only need to display the images of a star four times in a loop, and so on. Of course, when the workload is small, you will not feel the benefits of ASP. However, once the data volume increases dramatically, you will have a profound understanding of ASP dynamic websites that bring you unprecedented lightness and coolness!

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.