Since Carl needs to use my program, we work together. However, the program he wrote is Python, and the program I wrote is Java, so he must find a way to communicate. Despite Jython, Carl believes that CGI is the easiest. As a result, I learned how to create CGI a while ago. At the beginning, I felt that it was not very difficult, but the progress was not as smooth as expected. Finally, because the college server has a CGI Module but it is not allowed to run CGI at will, the lab server is installed with FastCGI, Which is troublesome. Finally, Carl said that socket should be used...
--|.
In general, the program to communicate with CGI went bankrupt. Although it is a very old and basically never used again, it is naturally a pity because it is a new thing for me. Although CGI was not used in the end, I personally think it should be okay if the college server allows users to run CGI on their own. In any case, record some basic knowledge to avoid forgetting it later.
CGI has a long history, but it has implemented dynamic web pages. CGI, to be precise, should be a protocol (or interface) that enables programs (CGI scripts) in the server to pass through standard I/O streams (stdin and stdout, for example, in Java, It is system. in and system. in C, it is printf. As long as it is a program capable of reading and writing standard I/O streams, it can be used to implement CGI) to read the required information and output information to the browser, the information to be read from the client is contained in some defined environment variables. We only need to read these environment variables, naturally, you can get the desired value (that is, what CGI is doing for us ). Generally speaking, cgi may also refer to CGI programs, that is, CGI.
A script is a program written by the developer to process user requests.
In general, the process is as follows:
1. The server receives the request
2. The server finds that this request requires a CGI program for processing.
3. The server establishes an environment in which there are some variables, that is, the environment variables required by the CGI program.
4. The server starts the CGI program in this environment.
5. The CGI program parses the desired environment variable to obtain the submitted request information.
6. The CGI program processes the request information accordingly.
7. The CGI program outputs the corresponding results to the standard output stdout, Which is output to the user end and presented to the user.
8. After the CGI program is executed, exit the program.
In details, the principle is like this:
1. When the client browses a webpage, it submits a form ). The Form Action specifies the CGI program to process the form, for example:
<HTML>
<Body>
<Form action = "/cgi-bin/plus. cgi" method = "get">
<Input name = "M" size = "5">
<Input name = "N" size = "5"> <br>
<Input type = "Submit" values = "Submit">
</Form>
</Body>
</Html>
In the above program, Action = "/cgi-bin/plus. CGI means that "all the data in this form is handed over to plus. the CGI program processes the data, and then it processes the data, and then returns the result to the browser ".
It must be noted that plus is the name of an executable file. For example, it may be a shell file (such as plus. Sh), and its suffix is not necessarily. cgi. So why should we name it. cgi here? The reason is that the form is processed by a program of Type CGI, or by using CGI to process the submitted data. Generally, out of security concerns, not all users have the permission to write their own CGI program and then upload it to the server to run it (for example, the server of our school won't work, ah ...). However, only executable files placed in the directory specified by the server (generally cgi-bin) can be considered as CGI programs and executed. However, most people cannot access this directory.
2. then, the two variables whose names are "M" and "N" and their values are input to the CGI program through an environment variable, the CGI program executes the processing specified by the CGI program. Get and post obtain "M" and "n" in different ways. If the submit method is get, the content submitted by get is included in the environment variable named QUERY_STRING. After obtaining the value of this environment variable, parse the content to get the correct value such as name/value, and then you can perform corresponding processing. In post mode, data needs to be read from the stdin of the standard input stream. How much data is there? You need to read the value of the content_length environment variable.
For example, in C, you can use the getenv function to directly obtain the value of the environment variable, such as getenv ("QUERY_STRING") and getenv ("content_length. Perl may be the best language for writing CGI, but it won't be Perl. skip this step. In addition, because my program is written in Java, it makes the way to read environment variables a little special, because JAVA does not directly obtain these environment variables. Why can't Java directly access environment variables? To put it simply, Java also has an internal attribute that belongs to the system class. Therefore, a system. getproperty obtains the attributes of the system class in Java, rather than environment variables. These system attributes store information about the Java Runtime Environment in the form of name/value. Therefore, the additional environment variables (or attribute information) need to use the command Java
-D format. For details, see the wrapper program later.
Common environment variables include request_method, QUERY_STRING, content_length, and path_info. Others include server_software, SERVER_NAME, gateway_interface, server_protocol, server_port, path_translated, script_name, remote_host, remote_addr, auth_type, remote_user, remote_ident, and content_type. If you forget what these are, let's get to Google...
Now, we have read the desired environment variable. However, the values of these environment variables need to be parsed, because when we first passed them to us, we performed URL encoding (URL encode ). For example, if the form we submit is of the get type, after the information is submitted, you can see in the address bar like this URL: http://www.xxxxxxx.edu/cgi-bin/plus.cgi? M = 5 & n = 6. "M = 5 & n = 6" indicates the encoded string, that is, the value we want to obtain. "Http://www.xxxxxxx.edu/cgi-bin/plus.cgi.pdf" is the address of the cgiprogram that processes the table ticket. "?" The following string (M = 5 & n = 6) is the input parameter. It is not hard to see that, during encoding, such rules are similar to "name1 = value1 & name2 = value2 & name3 = value3 ". In addition, non-Spanish strings will be replaced by "% xx", and spaces will be replaced by "+. For more information, see URL
Information about encode.
How to parse these troublesome strings does not need to be manually written. There are many such function libraries and languages on the Internet. Simply use the ready-made online function library for parsing. For Java, the cgi_lib.java library is recommended. Very convenient, widely used, and very small files. The functions are the same as those of cgi_lib.pl.
3. After parsing the string, we can know that the value of Variable M is 5, and the value of Variable N is 6. Then you can perform corresponding processing in the CGI program. Do what you want. For example, if you want to process addition, You can implement addition of M and N in your CGI program.
4. After the data is processed, the result must be presented to the browser user. This is simple, just use stdout. For example, the C language uses printf and the Java language uses system. Out. println. Note that the first output must be "Content-Type: text/plain" (that is, MIME header information, tell the server that its subsequent output is in the form of plain ASCII text), then empty line (must be empty line) below, and then print the HTML statement to be presented to the user. For example, in Java:
System. Out. println ("Content-Type: text/plain ");
System. Out. println ("");
For example, if you want to test whether you have the permission to write a CGI program and run it on the server, you can write a simple CGI program that does not process any user requests, only output "Hello, world ".
The shell program can be written as follows:
#! /Bin/sh
Echo "Content-Type: text/html"
Echo ""
Echo "<HTML>"
Echo "<body>"
Echo "Echo "</body>"
Echo "The C language can be written as follows:
# Include <stdio. h>
Void main (void ){
Printf ("Content-Type: text/html \ n ");
Printf ("\ n ");
Printf ("<HTML> \ n ");
Printf ("<body> \ n ");
Printf ("Printf ("</body> ");
Printf ("/<HTML> ");
}
In this way, the "Hello, world" in the Special notes for Java:
1. Java can only read environment variables through the-D option.
2. The. Class file generated by Java (such as java_plus.class) is not an executable file in the general sense. This file is only executable through JVM. The CGI program is a file that can be executed directly. Therefore, we need to write a wrapper to encapsulate this. Class file. When the wrapper program is called, the wrapper program can run the. Class file through Java commands.
Combined with appeal, we can write a shell file as a wrapper program, as shown below:
#! /Bin/sh
Java \
-Dcgi. content_type = $ content_type \
-Dcgi. content_length = $ content_length \
-Dcgi. request_method = $ request_method \
-Dcgi. QUERY_STRING = $ QUERY_STRING \
-Dcgi. SERVER_NAME = $ SERVER_NAME \
-Dcgi. server_port = $ server_port \
-Dcgi. script_name = $ script_name \
-Dcgi. path_info = $ path_info \
Java_plus
Save the above file as plus. SH and put it in the specified directory (such as cgi-bin. In this way, when a new form is submitted, the file named plus will be searched in this directory (because the suffix is CGI), and the file will be executed. In the java_plus file, we can use system. getproperty ("CGI. QUERY_STRING") to access the QUERY_STRING environment variable.
Recommended links:
Http://www.jmarshall.com/easy/cgi/
Http://www.eli.sdsu.edu/courses/spring96/cs596/notes/andrew/cgi.html
Http://www.javaworld.com/javaworld/jw-01-1997/jw-01-cgiscripts.html
Http://apps.hi.baidu.com/share/detail/18814484
Http://www.jaguarpc.com/forums/showthread.php? T = 2553
Http://httpd.apache.org/docs/current/howto/cgi.html
FastCGI is much more efficient than traditional CGI. However, it is troublesome to use loops and set ports or something. I just read the relevant information. Put it here.
Http://www.phpchina.com/download/handbook/linux-html/1272.html
Http://www.20cn.net/ns/wz/net/data/20030615005558.htm
Http://www.fastcgi.com/devkit/doc/fcgi-java.htm
Http://www.citycat.ru/doc/FastCGI/fcdk/index.html
Http://hi.baidu.com/coffeefoam/blog/item/1446493be749f3e814cecbb8.html