CGI Principle 2 ------ interaction data between WEB Services and CGI, cgi ------ web

Source: Internet
Author: User
Tags dsn web database

CGI Principle 2 ------ interaction data between WEB Services and CGI, cgi ------ web

# Include <stdio. h>
# Include <string. h>
# Include <unistd. h>
# Include <sys/wait. h>
# Include <stdlib. h>

# Define CGI_NAME "get_post.ums"
# Define REQUEST_METHOD "REQUEST_METHOD = POST"
# Define REQUEST_PARAMETER "myname = huangzhihui"

Int main (int argc, char * argv [])
{
Int fd [2];
If (pipe (fd) <0)
{
Printf ("create pipe fail. \ n ");
}

Pid_t pid;
If (pid = fork () <0)
{
Printf ("fork fail. \ n ");
}
Else if (pid> 0)
{
/* Parent */
// Simulate data transfer to CGI
Ssize_t length = strlen (REQUEST_PARAMETER );
If (write (fd [1], REQUEST_PARAMETER, length )! = Length)
{
Printf ("write error to pipe \ n ");
}
Close (fd [1]);

// Wait until the CGI sub-process completely reads the data and writes the data,
// Select or epoll listeners are used.
Usleep (1000 );

// Simulate receiving CGI response data
Char buff [256] = {0 };
Length = read (fd [0], buff, sizeof (buff ));
If (length <= 0)
{
Printf ("read error from pipe \ n ");
}
Else
{
Printf ("pid % d read data = % u, % s \ n", getpid (), length, buff );
}
Close (fd [0]);

If (waitpid (pid, NULL, 0) <0)
{
Printf ("waitpid error \ n ");
}
Exit (0 );
}
Else
{
/* Child */
// Redirect the input of the MPs queue to the standard input.
If (fd [0]! = STDIN_FILENO)
{
If (dup2 (fd [0], STDIN_FILENO )! = STDIN_FILENO)
{
Printf ("dup2 error to stdin ");
}
Close (fd [0]);
}

// Redirect the output of the MPs queue to the standard output
If (fd [1]! = STDOUT_FILENO)
{
If (dup2 (fd [1], STDOUT_FILENO )! = STDOUT_FILENO)
{
Printf ("dup2 error to stdout ");
}
Close (fd [1]);
}

// Overwrite the process space and set CGI Environment Variables
Char content_length [128] = {0 };
Sprintf (content_length, "CONTENT_LENGTH = % u", strlen (REQUEST_PARAMETER ));
Char * exec_argv [3] = {REQUEST_METHOD, content_length };
If (execve (CGI_NAME, argv, exec_argv) <0)
{
Printf ("execl error for % s", CGI_NAME );
}
Exit (0 );
}
Exit (0 );

}






What does the CGI program mean?

1. CGI: Common Gateway Interface
CGI stands for the Common Gateway Interface, which makes it possible to run some applications (or gateways) outside the network server. The CGI-BIN directory is where the CGI script is stored. These scripts enable the WWW server and browser to run external programs without starting another program.

It is a program running on a Web server and triggered by a visitor. CGI is an interface for running external programs (or gateways) on the HTTP server. It allows network users to access the application type programs on the remote system, it's like they are actually using those remote computers.

CGI allows viewers to interact with the server. If you have ever entered a table or searched on the network, it is very likely that CGI is used.

Although CGI is easy to use, when a large number of people use a CGI application at the same time, the response will be slow, and the speed of the network server will be greatly affected. CGI applications can run independently.

CGI applications can be written in most programming languages, such as Perl (Practical Extraction and Report Language), C \ C ++, Java, and Visual Basic. However, for web page producers who do not have much programming experience, it is a great challenge.

The CGI application works like this:

1. a browser request through an HTML form or hyperlink refers to the URL of the previous CGI application.

2. the server sends and receives requests.

3. The server executes the specified CGI application.

4. The operations required for CGI application execution are usually based on the content of the visitor.

5. The CGI application formats the result as a document that can be understood by the Web server and the browser (usually an HTML webpage ).

6. The network server returns the result to the browser.

Note the following for CGI applications:

The CGI application runs on the server system that the browser can request. The CPU time and memory of the server are used for execution. If tens of thousands of such programs run at the same time, it will impose extremely high requirements on the server system. Consider this issue with caution to prevent server system crashes.

Imperfect CGI applications may become channels for illegal entry into the human server system, which may lead to deletion or leakage of important information.

CGI applications are mainly used in the following ways:

Send a custom response based on the HTML form entered by the browser.

Create a clickable Image zoom-out chart.

Create a database where viewers can search for content.

Provides interfaces between servers and databases, and converts the results into HTML documents.

Create a dynamic HTML document.

If a CGI script can do the same thing on every computer, it will be easy to write the script. Unfortunately, CGI scripts depend on the operating system of the server. Therefore, it is useless for non-UNIX servers to use the top five (a common tool for writing scripts in UNIX) scripts. Therefore, you must customize and install your CGI script.

Most servers provide CGI-BIN directories, but this is not enough. Because you should have your own CGI-BIN. In this way, you can run your own scripts (instead of adapting your system to the scripts that already exist on the system ). Therefore, your provider should install CGI-BIN and be able to help you write scripts

Ii. ASP: Active Server Pages
ASP, an Active Server Page, is a programming environment in which HTML, scripting language, and components can be used together to create powerful Internet applications on the server side. If you have created a site that contains HTML, scripting language, and components, you can add ASP program code to it. By adding script commands to the HTML page, you can create an HTML user interface and use components to include some commercial logic rules. A component can be called by a script or other components.
How ASP works:

What is the general process of web database access?

Statement... this is what I transferred from another website...

Abstract: This article discusses several methods to access WEB databases on the WEB page: Using ASP, using IDC + HTX, using JDBC, and using CGI. Specific program implementation is provided for the first two types, and the latter two types are briefly introduced. Keyword WEB Database asp idc jdbc cgi with the development of computer technology, WEB has been widely used. WWW pages have gradually evolved from static pages to dynamic interactive pages, and the connection between WWW pages and databases has become increasingly important. At the early stage of WWW, you can access the database through the general Gateway Interface (CGI). Later, many vendors developed new interface technologies to make WEB database access more concise, convenient, and efficient. This article will introduce several methods for accessing data in WWW, focusing on the new ASP and IDC technologies, and will only briefly introduce JDBC and CGI. Assume that the operating system of the database Server is Windows NT Server 4.0, and the database is Microsoft SQL Server6.5. There is a database named example, a table named student, which stores age, gender, and other information, now we will discuss how to access it on the webpage. 1. Using ASP to access WEB databases Microsoft has launched Active ServerPages (ASP) Since IIS3.0, which is a major improvement in WEB technology. Traditional static pages must manually edit HTML pages to change the content that the WEB server sends to the browser. ASP can be used to dynamically generate WEB pages on the server. In addition, compared with CGI and ISAPI methods, ASP does not need other design environments. You can directly include executable scripts in HTML files to combine HTML development and Script Development, this greatly simplifies the development of WEB applications. The ASP running process is described as follows: When a browser requests an ASP file from the WEB server, the ASP script starts to run. The WEB server starts to run the ASP script from the beginning to the end and returns the HTML page to the browser. If you want to access a WEB database through ASP, the WEB Server accepts browser requests, sends the requests to the database server through ODBC or OLEDB, and then receives data from the database server, send the generated webpage and data back to the browser. In ASP, database access is implemented through ADO (ActiveX DataObjects). This method is easy to use and highly efficient. It is almost easy for programmers who have experience programming VB databases. Next let's take a look at the ASP file code of the complete web page (comment after '): Asp file Example. asp <% @ LANGUAGE = "VBSCRIPT" %> <% dim SQL, con, rs 'Open the database connection SQL = "DSN = HPEDB; UID = sa according to the DSN and database permissions; PWD = 1; DATABASE = hpedb "Set Con = Server. createObject ("ADODB. connection ") Con. open (SQL) 'executes queries according to SQL statements and returns the result set SQL = "select * from student where age = 23" set rs1_con.exe cute (SQL) %>

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.