环境:
apache-tomcat-6.0.33
jdk1.6.0_29
python-2.7
1. 配置CGI拦截路径
打开$(CATALINA_HOME)/conf/web.xml, 去掉有关CGI的注释
<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>cgiPathPrefix</param-name>
<param-value>WEB-INF/cgi-bin</param-value> <!-- 与url-pattern配置项一致 -->
</init-param>
<init-param>
<param-name>executable</param-name>
<param-value>D:\Python27\python.exe</param-value> <!-- python执行路径 -->
</init-param>
<init-param>
<param-name>passShellEnvironment</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
2. 配置权限
打开$(CATALINA_HOME)/conf/context.xml, 添加代码 <Context privileged="true">
3. 建立CGI工程
在$(CATALINA_HOME)/webapps下建立如下目录层次:
web.xml 拷贝至 $(CATALINA_HOME)/webapps/ROOT/web.xml, 在cgi-bin目录下新建simple.cgi
#!D:\Python27\python.exe
import cgitb; cgitb.enable() #enable debug
print 'Content-type: text/html'
#print 1/0
print 'Hello world'
4. 测试