1、修改conf/web.xml
找到並將cgi的servlet和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>
注意:其中cgiPathPrefix是cgi文件的存放路徑,默認是WEB-INF/cgi,我為了方面用awstats,所以改成cgi-bin
<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
/cgi-bin/*意思是,訪問cgi文件的默認地址為cgi-bin/*
2、修改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>
修改<Context>添加privileged="true"屬性,否則頁面提示Servlet CGI is not availabled,且tomcat會有錯誤提示java.lang.SecurityException: Restricted classclass org.apache.catalina.servlets.CGIServlet
3、如果tomcat的版本是6以下的,必須將server/lib/目錄下的servlets-cgi.renametojar改名為servlets-cgi.jar
有以上三步,已經可以成功配置CGI,報錯解決!