Let Tomcat support PHP when responsible for a website using JSP dynamic web page technology, just have a part of the function written by PHP, so it is very straightforward to think that Tomcat supports PHP. Tomcat is also from Apache. does Tomcat only support JSP? Can I use PHP on Tomcat? I searched the internet and soon got the answer to the first question: Tomcat can support CGI, such as Perl (see the appendix for specific settings ). However, it is too late to find a method for Tomcat to use PHP. generally, Apache and Tomcat are installed to coexist to build a so-called Web platform supporting PHP + JSP.
Of course, PHP itself also supports CGI, so I tried it myself (on a Windows operating system): first let Tomcat support CGI according to the appendix, then modify the web. xml, add the following configuration in the servlet-name cgi section:
<Init-param>
<Param-name> executable
<Param-value> php
Add the PHP installation plugin to the pathpath. In this example, Tomcat can run php.exe. Restart Tomcat, create a new cgi directory under the WEB-INF Directory, put the php file here, and then access these php files with the cgi-bin directory that is virtualized ING.
However, if you do not add any changes to the PHP file, you may find no output. You need to add a line in the PHP file header and output two carriage returns:
Echo "\ n ";
The reason is unclear. It may be waiting for the Content-type input or the cgi program style of Perl.
However, in PHP (version 4.1.0 or later), variables such as $ _ REQUEST, $ _ GET, and $ _ POST cannot be used, queryString can only be obtained from SERVER variables or environment variables: $ _ SERVER ["QUERY_STRING"], $ _ ENV ["QUERY_STRING"]. It may not support form POST submission ...... I have not tried it yet.
I am not familiar with Tomcat, so these are just minor repairs on the current method. There may be good practices. I hope you will not give me any advice!
Appendix:
Using CGI Scripts with Tomcat
Tomcat is primarily meant to be a servlet/JSP container, but it has 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 implements des 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 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. restart Tomcat, and your CGI processing shoshould 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 in 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.