在負(fù)責(zé)一個(gè)采用JSP動(dòng)態(tài)網(wǎng)頁(yè)技術(shù)的網(wǎng)站時(shí),剛好手頭有PHP寫(xiě)的一部分功能,所以就很直接地想到讓Tomcat支持PHP。Tomcat也是Apache出的,難道就只支持JSP嗎?到底能否在Tomcat上用PHP呢?上網(wǎng)搜索了一番,很快有了第一個(gè)問(wèn)題的答案:Tomcat可以支持CGI,如Perl(具體設(shè)置見(jiàn)附文)。但繼續(xù)搜索,卻遲遲無(wú)法找到讓Tomcat使用PHP的方法,一般都是采取裝Apache和Tomcat共存的方法來(lái)搭建所謂的支持PHP+JSP的Web平臺(tái)。
當(dāng)然,其實(shí)PHP本身也支持CGI方式的使用,所以就自己動(dòng)手試了一試(在Windows操作系統(tǒng)上):先按照附文的做法讓Tomcat支持CGI,然后再改web.xml,在servlet-name為cgi的一段配置中加上:
????
<
init-param
>
????????
<
param-name
>
executable
</
param-name
>
????????
<
param-value
>
php
</
param-value
>
????
</
init-param
>
并把PHP安裝路徑加入Path,這樣Tomcat就能運(yùn)行到PHP.exe了。重啟Tomcat后,在WEB-INF目錄下新建一個(gè)cgi目錄,把php文件放在這里,然后訪問(wèn)時(shí)用虛擬映射出來(lái)的cgi-bin目錄來(lái)訪問(wèn)這些PHP文件。
但是如果對(duì)PHP文件不加任何改動(dòng)的話,可能會(huì)發(fā)現(xiàn)什么輸出都沒(méi)有。需要在PHP文件頭部加一行,輸出兩個(gè)回車:
echo?
"
\n\n
"
;
原因不太清楚。可能在等待Content-type輸入,或是Perl的cgi程序風(fēng)格。
但這樣配置,PHP(4.1.0以上版本)中$_REQUEST、$_GET、$_POST等變量就無(wú)法用了,只能從服務(wù)器變量或環(huán)境變量中獲取QueryString了:$_SERVER["QUERY_STRING"]、$_ENV["QUERY_STRING"]。也許不支持表單的POST提交了……具體還沒(méi)試過(guò)。
由于對(duì)Tomcat不熟悉,所以這些只是在現(xiàn)在方法上的小修小補(bǔ),可能有好的做法,還希望大家不吝指教!
附:
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:
當(dāng)然,其實(shí)PHP本身也支持CGI方式的使用,所以就自己動(dòng)手試了一試(在Windows操作系統(tǒng)上):先按照附文的做法讓Tomcat支持CGI,然后再改web.xml,在servlet-name為cgi的一段配置中加上:




但是如果對(duì)PHP文件不加任何改動(dòng)的話,可能會(huì)發(fā)現(xiàn)什么輸出都沒(méi)有。需要在PHP文件頭部加一行,輸出兩個(gè)回車:

但這樣配置,PHP(4.1.0以上版本)中$_REQUEST、$_GET、$_POST等變量就無(wú)法用了,只能從服務(wù)器變量或環(huán)境變量中獲取QueryString了:$_SERVER["QUERY_STRING"]、$_ENV["QUERY_STRING"]。也許不支持表單的POST提交了……具體還沒(méi)試過(guò)。
由于對(duì)Tomcat不熟悉,所以這些只是在現(xiàn)在方法上的小修小補(bǔ),可能有好的做法,還希望大家不吝指教!
附:
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.
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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