让Tomcat支持PHP

系统 1737 0
  在负责一个采用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 " ;
原因不太清楚。可能在等待Content-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, 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:
  1. 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 .
  2. 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).
  3. 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.
  4. 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 the CGIServlet 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.
  5. Restart Tomcat, and your CGI processing should now be operational.
The default directory for the servlet to locate the actual scripts is WEB-INF/cgi . As has been noted, the WEB-INF directory is protected against casual snooping from browsers, so this is a good place to put CGI scripts, which may contain passwords or other sensitive information. For compatibility with other servers, though, you may prefer to keep the scripts in the traditional directory, /cgi-bin , but be aware that files in this directory may be viewable by the curious web surfer. Also, on Unix, be sure that the CGI script files are executable by the user under which you are running Tomcat.

让Tomcat支持PHP


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论