在負責一個采用JSP動態網頁技術的網站時,剛好手頭有PHP寫的一部分功能,所以就很直接地想到讓Tomcat支持PHP。Tomcat也是Apache出的,難道就只支持JSP嗎?到底能否在Tomcat上用PHP呢?上網搜索了一番,很快有了第一個問題的答案:Tomcat可以支持CGI,如Perl(具體設置見附文)。但繼續搜索,卻遲遲無法找到讓Tomcat使用PHP的方法,一般都是采取裝Apache和Tomcat共存的方法來搭建所謂的支持PHP+JSP的Web平臺。
當然,其實PHP本身也支持CGI方式的使用,所以就自己動手試了一試(在Windows操作系統上):先按照附文的做法讓Tomcat支持CGI,然后再改web.xml,在servlet-name為cgi的一段配置中加上:
????
<
init-param
>
????????
<
param-name
>
executable
</
param-name
>
????????
<
param-value
>
php
</
param-value
>
????
</
init-param
>
并把PHP安裝路徑加入Path,這樣Tomcat就能運行到PHP.exe了。重啟Tomcat后,在WEB-INF目錄下新建一個cgi目錄,把php文件放在這里,然后訪問時用虛擬映射出來的cgi-bin目錄來訪問這些PHP文件。
但是如果對PHP文件不加任何改動的話,可能會發現什么輸出都沒有。需要在PHP文件頭部加一行,輸出兩個回車:
echo?
"
\n\n
"
;
原因不太清楚??赡茉诘却鼵ontent-type輸入,或是Perl的cgi程序風格。
但這樣配置,PHP(4.1.0以上版本)中$_REQUEST、$_GET、$_POST等變量就無法用了,只能從服務器變量或環境變量中獲取QueryString了:$_SERVER["QUERY_STRING"]、$_ENV["QUERY_STRING"]。也許不支持表單的POST提交了……具體還沒試過。
由于對Tomcat不熟悉,所以這些只是在現在方法上的小修小補,可能有好的做法,還希望大家不吝指教!
附:
Using CGI Scripts with Tomcat
Tomcat is primarily meant to be a servlet/JSP container, but it has many capabilities rivalling a traditional web server. One of these is support for the Common Gateway Interface (CGI), which provides a means for running an external program in response to a browser request, typically to process a web-based form. CGI is called "common" because it can invoke programs in almost any programming or scripting language: Perl, Python,
Tomcat includes an optional CGI servlet that allows you to run legacy CGI scripts; the assumption is that most new back-end processing will be done by user-defined servlets and JSPs.
To enable Tomcat's CGI servlet, you must do the following:
當然,其實PHP本身也支持CGI方式的使用,所以就自己動手試了一試(在Windows操作系統上):先按照附文的做法讓Tomcat支持CGI,然后再改web.xml,在servlet-name為cgi的一段配置中加上:




但是如果對PHP文件不加任何改動的話,可能會發現什么輸出都沒有。需要在PHP文件頭部加一行,輸出兩個回車:

但這樣配置,PHP(4.1.0以上版本)中$_REQUEST、$_GET、$_POST等變量就無法用了,只能從服務器變量或環境變量中獲取QueryString了:$_SERVER["QUERY_STRING"]、$_ENV["QUERY_STRING"]。也許不支持表單的POST提交了……具體還沒試過。
由于對Tomcat不熟悉,所以這些只是在現在方法上的小修小補,可能有好的做法,還希望大家不吝指教!
附:
Using CGI Scripts with Tomcat
Tomcat is primarily meant to be a servlet/JSP container, but it has many capabilities rivalling a traditional web server. One of these is support for the Common Gateway Interface (CGI), which provides a means for running an external program in response to a browser request, typically to process a web-based form. CGI is called "common" because it can invoke programs in almost any programming or scripting language: Perl, Python,
awk
, Unix shell scripting, and even Java are all supported options. However, you probably wouldn't run a Java application as a CGI due to the start-up overhead; elimination of this overhead was what led to the original design of the servlet specification. Servlets are almost always more efficient than CGIs because you're not starting up a new operating-system-level process every time somebody clicks on a link or button.
Tomcat includes an optional CGI servlet that allows you to run legacy CGI scripts; the assumption is that most new back-end processing will be done by user-defined servlets and JSPs.
To enable Tomcat's CGI servlet, you must do the following:
-
Rename the file
servlets-cgi.renametojar
(found in
CATALINA_HOME/server/lib/
) to
servlets-cgi.jar
, so that the servlet that processes CGI scripts will be on Tomcat's
CLASSPATH
. -
In Tomcat's
CATALINA_BASE/conf/web.xml
file, uncomment the definition of the servlet named
cgi
(this is around line 241 in the distribution). -
Also in Tomcat's
web.xml
, uncomment the servlet mapping for the
cgi
servlet (around line 299 in the distributed file). Remember, this specifies the HTML links to the CGI script. -
Either place the CGI scripts under the
WEB-INF/cgi
directory (remember that
WEB-INF
is a safe place to hide things that you don't want the user to be able to view, for security reasons), or place them in some other directory within your context and adjust the
cgiPathPrefix
initialization parameter of theCGIServlet
to identify the directory containing the files. This specifies the actual location of the CGI scripts, which typically will not be the same as the URL in the previous step. - Restart Tomcat, and your CGI processing should now be operational.
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
