Skip to main content

tomcat configuration cgi

Mr.ChenLess than 1 minutearticleJava

1.Modify conf/web.xml

Find and remove the comments of cgi's servlet and servlet-mapping

<servlet>
   <servlet-name>cgi</servlet-name>
   <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
   <init-param>
     <param-name>debug</param-name>
     <param-value>0</param-value>
   </init-param>
   <init-param>
     <param-name>cgiPathPrefix</param-name>
     <param-value>cgi-bin</param-value>
   </init-param>
   <load-on-startup>5</load-on-startup>
</servlet>

Note: where cgiPathPrefix is the storage path of cgi files, the default is WEB-INF/cgi, I use awstats for the sake of convenience, so I changed it to cgi-bin

<servlet-mapping>
   <servlet-name>cgi</servlet-name>
   <url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>

/cgi-bin/means that the default address for accessing cgi files is cgi-bin/

2.Modify conf/context.xml

<?xml version='1.0' encoding='utf-8'?>
<Context privileged="true">
   <!-- Default set of monitored resources -->
   <WatchedResource>WEB-INF/web.xml</WatchedResource>
   <!-- Uncomment this to disable session persistence across Tomcat restarts -->
   <!-- <Manager pathname="" /> -->
   <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) -->
   <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> -->
</Context>

Modify <Context> to add the privileged="true" attribute, otherwise the page will prompt Servlet CGI is not available, and tomcat will have an error prompt
java.lang.SecurityException: Restricted classclass org.apache.catalina.servlets.CGIServlet

  1. If the version of tomcat is below 6, you must rename the servlets-cgi.renametojar in the server/lib/ directory to servlets-cgi.jar

With the above three steps, CGI can be successfully configured, and the access address is http://localhost:8080/cgi-bin/test.pl