Jsp http server supports CGI. In short, it supports the client to call the CGI routine of the server. On the server side, CGI is generally stored as executable files, such as executable files and executable scripts ). These CGI execution files can be run independently or the client requests can be run as parameters. The HTTP server needs to call the CGI executable file, pass the Request Parameters of the client to the CGI program, and feedback the CGI execution result to the client.
1. Call of CGI programs that do not require parameters on the jsp http Server
For CGI programs that do not require parameters, the function module has been fixed to the CGI program. When called, you can execute this function module. This type of CGI program is generally used in scenarios with relatively fixed functions. The following code supports both CGI that requires parameters and CGI that does not require parameters:
- //Parameter is null
- if(params.equals("") == true) {
- command = fileName;
- }
- else //Generate the command line {
- command = fileName + " " + params;
- }
- //Execute CGI process as child process Process proc = m_rt.exec(command, null);
- //Create CGI process output stream
- BufferedReader pin = new BufferedReader(new InputStreamReader(proc.getInputStream() ) );
- String line = null;
- //Get the output from CGI process
- while( (line = pin.readLine() ) != null) {
- m_sout.println(line);
- }
- //Wait for CGI process finish
- proc.waitFor();
In the above Code, the CGI executable file is called through the exec interface of the Java Runtime class, And the CGI execution output is returned to the client.
2. Call of CGI programs whose parameters are required by the jsp http Server
For CGI programs that require parameters, the function modules are not fixed. Instead, you need to input parameters to execute the corresponding functions. This type of CGI program is relatively flexible. When called, the HTTP server must pass the parameter lines in the client request into the CGI process, parse the request in the CGI program, and then execute the corresponding module. The communication between the HTTP server and CGI process can be implemented in three ways:
1) input the execution parameters in command line mode. See the above Code "command = fileName +" "+ params. Use the executable files and parameters as command lines to pass in the call.
2) written to the standard input stdin of the CGI process. The CGI program only needs to read the corresponding parameter lines from the standard input.
3) write the parameter lines to the environment variable QUERY_STRING of the CGI process. CGI reads the command line parameters from the environment variable QUERY_STRING.
- Get database connection in JSP
- Introduction to JSP Action
- Simplified code in JSP expressions
- Detailed explanation of JSP-to-Servlet Conversion
- Introduction to JSP Elements