標籤:cgi c tomcat privileged executable
基於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/shecho "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;}
測試結果: