C、Shell、Perl基于Tomcat开发CGI程序环境配置

基于Tomcat7.0版本配置CGI开发环境,步聚如下:

以我的Tomcat7安装目录为例:TOMCA_HOME = /Users/yangxin/Documents/devToos/java/apache-tomcat-7.0.39

1、打开TOMCA_HOME/conf/web.xml

将CGI的Serlvet配置与URL映射注释打开

<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>executable</param-name>
            <param-value></param-value>
        </init-param>
        <init-param>
          <param-name>cgiPathPrefix</param-name>
          <param-value>WEB-INF/cgi</param-value>
      </init-param>
         <load-on-startup>5</load-on-startup>
    </servlet>
<!-- The mapping for the CGI Gateway servlet -->

    <servlet-mapping>
        <servlet-name>cgi</servlet-name>
        <url-pattern>/cgi-bin/*</url-pattern>
    </servlet-mapping>
CGI Servlet初始化参数说明:

1> cgiPathPrefix:设置cgi程序在应用中的访问位置,默认访问位置为:应用名称/WEB-INF/cgi

2> executable:CGI程序解析器,默认为perl,如果为空,可以是任何安装在操作系统环境变量的脚本解析器,或是C/C++程序

3> parameterEncoding:访问CGI Servlet的默认参数编码,默认为utf-8

4> passShellEnvironment:是否开启shell环境变量,默认为false

5> stderrTimeout:读取标准错误信息超时时长,默认为2000毫秒



2、打开TOMCAT_HOME/conf/context.xml

在context节点上添加一个属性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>

3、创建CGI测试程序

在TOMCAT_HOME/webapps目录下创建建一个应用,如:cgitest,在cgitest应用下创建一个WEB-INF目录,在WEB-INF目录下创建一个cgi目录和一个web.xml文件,然后在cgi目录添加一个CGI测试脚本程序hello.sh和a.c并编译成a.cgi,并修改访问权限。随后启动tomcat,访问http://localhost:8080/cgitest/cgi-bin/hello.sh就可以访问自己写的CGI程序了

创建好的应用目录结构如下所示:


web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

</web-app>
hello.sh:

#!/bin/sh
echo "Content-type:text/html\n\n"
echo "hello world"
a.c

#include <stdlib.h>
#include <stdio.h>

int main(int argc, const char** args)
{
    printf("Content-type:text/html\n\n");
    printf("i is cgi programe");
    return 0;
}

测试结果:



posted @ 2014-07-12 22:02  奋斗+坚持  阅读(276)  评论(0编辑  收藏  举报