tomcat上部署CGI
CGI的定义是:外部应用程序与Web服务器之间的接口。
1.Tomcat7支持CGI,但是默认配置是关闭的需要进行如下配置
修改Tomcat conf/web.xml中两处代码,默认是注释掉的,去掉注释即可
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <servlet> 2 <servlet-name>cgi</servlet-name> 3 <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class> 4 <init-param> 5 <param-name>debug</param-name> 6 <param-value>0</param-value> 7 </init-param> 8 <init-param> 9 <param-name>cgiPathPrefix</param-name> 10 <param-value>WEB-INF/cgi</param-value> 11 </init-param> 12 <init-param> 13 <param-name>executable</param-name> 14 <param-value>D:/Dev/python/Python36/python.exe</param-value> 15 </init-param> 16 <init-param> 17 <param-name>passShellEnvironment</param-name> 18 <param-value>true</param-value> 19 </init-param> 20 <load-on-startup>5</load-on-startup> 21 </servlet>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <servlet-mapping> 2 <servlet-name>cgi</servlet-name> 3 <url-pattern>/cgi-bin/*</url-pattern> 4 </servlet-mapping>
2.修改Tomcat conf/context.xml
在标签<Context>中添加属性 privileged = "true" ,此时,Tomcat才允许web应用使用容器内的Servlet
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <Context privileged="true">
3.运行CGI程序
当一个CGI请求到达Tomcat时,CGIServlet会对其进行拦截,请求的格式为 http://.../cgi-bin/xxxx,然后CGIServlet会到相应的项目的WEB-INF/cgi目录下调用相应的CGI文件处理请求
4.测试
目录结构:
python_cgi
|---WEB-INF
|---cgi
|---hello.py
hello.py内容:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 print ("Content-type:text/html") 2 print () # 空行,告诉服务器结束头部 3 print ('<html>') 4 print ('<head>') 5 print ('<meta charset="GBK">') 6 print ('<title>Hello Word - 我的第一个 CGI 程序!</title>') 7 print ('</head>') 8 print ('<body>') 9 print ('<h2>Hello Word! 我是一个CGI程序</h2>') 10 print ('</body>') 11 print ('</html>')
在浏览器中输入:http://localhost:8080/python_cgi/cgi-bin/hello.py
输出结果:
Hello Word! 我是一个CGI程序