Introduction
CGI (Common Gateway Interface) defines a way of interaction between a Web server and an external content generation program. The external content generation program mentioned here is usually It is called a CGI program or CGI script.
When you use Tomcat as an HTTP server and you need CGI support, you can add CGI support in Tomcat. Tomcat’s CGI support is largely compatible with Apache’s httpd’s, but there are some limitations (for example, there is only one cgi-bin directory).
CGI support is achieved through the servlet class org.apache.catalina.servlets.CGIServlet
. Generally speaking, this servlet corresponds to the URL pattern “/cgi-bin/*”.
Tomcat does not support CGI by default.
Installation
Warning: CGI scripts are used to execute programs outside the Tomcat JVM. If you use Java’s SecurityManager, it will bypass the security policy configured in
catalina.policy
.
To enable CGI support:
-
In the default
$CATALINA_BASE/conf/web.xml
file, There are sample servlets and servlet-mapping elements for CGI servlets that are commented out. To enable CGI support in a web application, you need to copy the servlet and servlet-mapping declarations to theWEB-INF/web.xml
file of the web application. -
Set
privileged="true"
in the Context element of the web application.Only privileged contexts can be allowed to use CGI servlets. Note that modifying the global
$CATALINA_BASE/conf/context.xml
file will affect all web applications. Check the Context documentation for details.
Configuration
The following are some servlet initial parameters used to configure CGI servlet behavior:
- < li>cgiPathPrefix The path to search for CGI scripts, generally starting from the web application root directory + file. separator + this prefix. This parameter is empty by default, so that the root directory of the Web application is used as the search path. The recommended value is:
- debug The detail level of debugging information recorded by this servlet. The default is
0
. - executable The suffix of the executable file used to run the script. If the script itself is an executable file (such as an .exe file), you can explicitly set this parameter to empty String. The default is
perl
, that is, the default is a perl script. - executable-arg-1 and executable-arg-2, etc. Other parameters of
executable
. They are placed before the name of the CGI script. There are no other extra parameters by default. - parameterEncoding The name of the parameter encoding used by the CGI Servlet. The default is
System.getProperty("file.encoding","UTF-8")
. The system default encoding is preferred. If the system property is not available, UTF-8 encoding is used. - passShellEnvironment Should the shell environment variables (if any) of the Tomcat process be passed into the CGI script? The default is
false
. - stderrTimeout The time (in milliseconds) to wait for the standard error output (stderr) to be read before terminating the CGI process. The default is
2000
.
WEB-INF/cgi
.