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

"
echo "hello world"
a.c

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

int main(int argc, const char** args)
{
    printf("Content-type:text/html

");
    printf("i is cgi programe");
    return 0;
}

測试结果:



原文地址:https://www.cnblogs.com/blfshiye/p/5250026.html