1、在ROOT下的WEB-INF文件夹下新建文件夹cgi-bin
2、在cgi-bin文件夹下新建脚本文件cgitest.cgi
如:
print "Content-type:text/html"
print # 空行,告诉服务器结束头部
print '<html>'
print '<head>'
print '<meta charset="utf-8">'
print '<title>我的第一个 CGI 程序!</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word!</h2>'
print '</body>'
print '</html>'
3、修改web.xml和context.xml文件
a. 打开web.xml文件,找到这一段被注释的节点(如下),如果你从没自己修改过,那应该是被注释的,你还需要添加一些参数。
<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
<init-param>
<param-name>clientinputTimeout</param-name>
<param-value>100</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>passShellEnvironment</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cgiPathPrefix</param-name>
<param-value>WEB-INF/cgi-bin</param-value>
</init-param>
<init-param>
<param-name>executable</param-name>
<param-value>C:/Python26/python.exe</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
</servlet>
解释几个重要的参数:
“passShellEnvironment”: 与Python解析器解析CGI脚本有关,但是一定要配置好Python的环境变量;
“cgiPathPrefix”: 与Server能够访问的脚本目录有关,与第二步内容相对应;
“executable”: (这是我的安装路径)与Python解析器有关,没有解析器,Server怎么解析呢~
b. 找到第二段被注释的节点:
<servlet-mapping>
<servlet-name>cgi</servlet-name>//这个名称不能修改
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
这里的/cgi-bin/*指定了浏览器访问的地址
如:http://localhost:8080/cgi-bin/cgitest.cgi
c. 配置权限:
打开context.xml,添加:privileged="true"
<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>
window7 tomcat8 配置python27的cgi
最新推荐文章于 2025-01-07 17:05:52 发布