Use ASP to develop Web Applications

Source: Internet
Author: User
Tags dsn

Use ASP to develop Web Applications

Generally, web pages viewed by users through Browsers are mostly static. With the development of Web applications, users want to dynamically generate homepages based on requirements, for example, respond to user requirements for database query and generate reports.

The traditional methods for generating dynamic home pages based on user requests include CGI and ISAPI. CGI activates the response process based on the HTTP request of the browser. Each request corresponds to a process. When there are many requests at the same time, the program occupies system resources, resulting in low efficiency. ISAPI improves this shortcoming by Using DLL (Dynamic Link Library) technology to replace processes with threads, it improves performance and speed, but thread synchronization needs to be considered, and the development steps are cumbersome. There is another problem between these two technologies and Java, which is commonly used to develop dynamic web pages, that is, development difficulties, program Development and HTML writing are two completely different processes that require special programmer development. Simple development technologies, such as Objective C ript and IDC (Internet Database Connector), have limited functions and are not suitable for use.

ASP, ActiveX Server Page, is a new generation of dynamic web page development technology developed by Microsoft. It has the advantages of simple development and powerful functions, and can implement complex web applications intuitively and easily. This article introduces the basic concepts, features, and development elements of ASP, and introduces the specific implementation of Web application development using ASP through two typical examples.

ASP concepts and workflows

ASP is a web server development environment that can be used to generate and run dynamic, interactive, and high-performance Web service applications.

ASP is a server technology in ActiveX technology. Unlike common technologies that implement Dynamic Homepage on the client, such as ja va applet, ActiveX control, VB script, and JavaScript, commands and script statements in ASP are interpreted and executed by the server, the execution result generates a dynamically generated web page and sends it to the browser. The script command of the Client technology is interpreted and executed by the browser. Because ASP is interpreted and executed on the server side, developers do not have to consider whether the browser supports ASP. At the same time, because it is executed on the server side, developers do not have to worry about downloading the program order to steal the programming logic.

ASP is implemented through an ASP file suffixed with. asp. An ASP file is equivalent to an executable file, so it must be placed in a directory on the Web server with executable permissions.

When the browser requests ASP files from the Web server, ASP is started. The Web server calls ASP, reads the requested. asp file from the ground up, executes each command, dynamically generates an HTML page, and sends it to the browser. The production of ASP files is similar to that of HTML files. It is integrated with HTML development and can be completed in the same process. You can use the built-in ASP object and server component to complete complex tasks. You can also develop or use the server components developed by others to complete specialized tasks.

ASP has the following features:

Fully integrated with HTML;

Easy to generate, without manual compilation and connection;

Object-oriented and extensible ActiveX Server Components.

Currently, ASP only applies to the following Web servers:

IIS 3.0 on Windows NT;

Microsoft Peer Web Server V3.0 on NT Workstation;

Microsoft Personal Web server on Win95.

ASP files

1. asp File Creation

An ASP file is a text file with the suffix ". asp". It can contain any combination of the following elements:

Text)

HTML flag (TAGS)

Script command

The preparation of ASP files is very simple. You can use any unformatted text editing tool (I have been using notepad). You can also use specialized home development tools and ASP development tools such as Visual inetdev. See the following example:

Current Time: <% = now %> save it as a suffix. ASP files are stored in a directory specified by the Web server with execution right. After the file is linked, it can output the current time of the server. Note: ASP files cannot be executed by opening them, but must be links. For example, if ASP is a virtual directory with execution permission on the Web server (host name, to execute hello. ASP:

Http: // host/asp/Hello. asp

2. asp syntax

ASP is not a language. It only provides an environment for running scripts in ASP files. To use as P smoothly, ASP syntax rules must be followed. ASP syntax consists of the following elements:

Delimiters

Delimiters are symbols used to define a flag unit, such as "<" and ">" in HTML ".

Similarly, the commands and output expressions of ASP script have delimiters, which are different from those of text and HTML. The command delimiters are "<%" and "%> ". For example, the following is a value assignment statement:

<% Name = "timeout" %>

ASP uses "<%" = and "%>" to output expressions to the browser, for example:

<% = Name %> the browser outputs "timeout ".

Script flag

ASP can use any script language, as long as the corresponding Script driver (ENGINE) is provided, ASP itself provides VBScript and JScript drivers. Its default script language is VBScript. Of course, developers can change this default setting. For example, to change it to JScript, you only need to specify <% @ Language = JScript %> at the beginning of the file. Some of <s syntax> and </SCRIPT> are language programs, which are similar to HTML. The difference is that in HTML, this part is interpreted and executed by the browser, but in ASP, it is interpreted and executed by the Web server.

You can use several different script languages in A. asp file. You only need to enclose each segment with <scriptlanguage = La nguagename> and </SCRIPT>. The statement can also be included in the script executed by the browser, and the description statement can be annotated and interpreted and executed by the browser.

HTML Tag:

The ASP file can contain various expressions in the HTML language.

ASP built-in object

ASP provides five built-in objects to provide higher-level web functions. They are:

Request: obtain information from the user;

Response: send the information to the user;

Server: Provides Web server tools;

Session: User information stored in a session;

Application: allows different users to share information in an ASP application.

The first three objects are used at most. Request and response are used to implement web server and browser interaction. An important method of R equest is form (), which extracts user input information from the browser. For example, there is a text edit box on the home page. The variable "name" has been specified, and the form action is an ASP file. The following statement gets the content that the user fills in:

Request. form ("name ")

Obviously, this avoids the complex programming required by CGI and other methods.

Write:

Response. Write "Your message"

This statement outputs a message to the user's browser.

The server object has two important methods: mappath and Createobject. Mappath is used to restore the virtual pseudo path of the Web server to the actual path. Createobject is probably the most important method to generate an object instance of the server component. For details, see the following.

Server Components

Through scripts and HTML on the server side, you can easily generate dynamic web pages with limited functions, such as not connecting to the server database, using network functions, or accessing the Server File System. To solve this problem, we need to use the co m technology, namely the component object model. Almost all ActiveX technologies are based on this. Through COM, other COM components can be easily used. Such COM components that can be shared by the web are server components. A server component is equivalent to an object that provides attributes and methods to use server resources. Server components can be developed by any third party that supports Active X. asp itself also comes with five server components that can be directly used and can complete most of the work on the server end.

To call a server component, you must first use the Createobject method in the server object to generate an instance of the server component object, as shown below:

Server. Createobject (progid)

Here, progid specifies the component ID, which can be various forms of executable programs (DLL, EXE, etc.), and does not have to consider its location, as long as it is in Windows NT (or 95) to register these programs, com will maintain the information in the system data warehouse (Registry), at the same time, in the progid way for programmers to call. Register with the regsvr32 program. You can use the Regedit program to view the progid. After a component is generated, you can use its methods and attributes to work.

The program snippets using server components are as follows:

'Generate component

Setobj = server. Createobject ("progid ")

'How to use it

OBJ. Method

ASP provides five server components, two of which are the database access component ADODB and the file access component F ilesystemobject. The example in this article describes their usage.

For some special requirements, developers can also develop their own server components. Using VB or Vc, developers can easily develop some components, and then register these components to use the server. createobject. There are also many websites with developed components for download, as long as they can be registered on their own machines.

ASP application instance

1. Use the file access component to create a home access counter

Many home pages now have a counter to record the number of times the home page is accessed. Usually, the counter is implemented through CGI, and the development process is complicated, which is difficult for some non-professional programmers. The file acce SS component (scripting. FileSystemObject), an ASP file access component, can easily generate a counter. The following is an example. The principle is: generate a counting File Based on page hits, read the numbers in the counting file, and dynamically call up an image that represents numbers such as 1, 2, and 3. Its basic process is similar to that of CGI, but it is different when sending an image. CGI uses a program to dynamically generate a whole image, our method is to call up images that already exist in the machine one by one, so that we can make very beautiful digital images. The program segment is:

<HTML>

<%

Countfile = server. mappath ("/gjy" + "/count.txt ")

'Count the file

Set fileobj = server. Createobject ("scripting. FileSystemObject") 'generates the file access component

Set out = fileobj. opentextfile (countfile, 1, false, false) 'open the file

Visitors = out. readline' read access records

Visitors = visitors + 1' plus one

Length = Len (visitors) 'the number of retrieved Images

Strurl = "" 'image URL

Do While length> = 1

Strurl = "

Length = length-1

Loop 'generate image URL

Set out = fileobject. createtextfile (countfile, true, true)

Out. writeline (visitors) 'number of records written back

%>

<% = Strurl %> 'output image

</Html>

Note: You can set the image file path based on your server.

2. Use Data Access Component (ADODB. Connection) to access the server database

Querying the backend database of the server through a browser is a required service for many web service providers. asp implements this function through the built-in ADODB component. ADO is the active data object. Like Dao and rdo, it belongs to the COM component of the data warehouse application. What's different is that ADO is specially developed for the Internet and web, this is also optimized. To query a database using ADO, follow these steps:

Set DSN

ADODB works through ODBC. Therefore, you must set the DSN (Data Source Name) in ODBC ).

Generate an ADODB component instance

Set connect = server. Createobject ("ADODB. Connection ")

Connect to database

Use the member function open of ADODB and the previously set DSN to connect to the database:

Connect. Open ("DSN = dsnname; uid = userid; Pwd = PASSWORD ")

Execute Query

Specify the SQL query statement:

SQL = select * From tablename

Run the query:

Set rs = connect. Execute (SQL)

Display result

Complete program snippets:

<%

Set connect = server. Createobject ("ADODB. Connection") 'to generate component instances

Connect. Open ("DSN = dsnname; uid = userid; Pwd = PASSWORD") 'connect to the database

SQL = select * From tablename

Set rs = connect. Execute (SQL) 'execute the query

%>

<% Do while not Rs. EOF %> 'display result

<% = RS (fieldname) %>

<%

Rs. movenext

Loop

%>
Conclusion
ASP development is simple and intuitive, and the development process can be easily integrated with HTML. With COM technology, more powerful and complex functions can be achieved.

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.