Let Tomcat support PHP

Source: Internet
Author: User
In charge of a Web site that uses JSP Dynamic Web technology, just have a part of PHP writing function, so it is very straightforward to think that Tomcat support PHP. Tomcat is also Apache out, is it only support JSP? Is it possible to use PHP on tomcat? Search the Internet and soon have the answer to the first question: Tomcat can support CGI, such as Perl (see enclosure for specific settings). But continue to search, but have been unable to find a way for Tomcat to use PHP, is generally taken to install Apache and Tomcat coexistence method to build so-called support php+jsp Web platform.
Of course, in fact, PHP itself also supports the use of CGI, so you try it yourself (on the Windows operating system): first follow the instructions of the enclosure to let Tomcat support CGI, and then change the Web. XML, in a servlet-name for the CGI configuration to add:

< Init-param >
< param-name > Executable
< Param-value > PHP

and add the PHP installation path to path so that Tomcat can run to PHP.exe. After rebooting Tomcat, create a new CGI directory under the Web-inf directory, place the PHP file here, and access the PHP files using the virtual mapped Cgi-bin directory.
But if you don't make any changes to your PHP files, you may find that you don't have any output. Need to add a line to the header of the PHP file, output two carriage returns:

echo "\ n";

The reason is not very clear. May be waiting for content-type input, or Perl's CGI program style.
However, this configuration, PHP (4.1.0 above) $_request, $_get, $_post and other variables can not be used, only from the server variables or environment variables to get querystring: $_server["Query_string"], $_ env["Query_string"]. The post submission of the form may not be supported ... I haven't tried it in detail yet.
Because of the Tomcat is not familiar with, so these are only in the current method of small repair, may have good practices, but also hope that everyone feel free!

Report:
Using CGI Scripts with Tomcat
Tomcat is primarily meant to being 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 is all supported options. However, probably wouldn ' t run a Java application as a CGI due to the start-up overhead; Elimination of this overhead is what led to the original design of the servlet specification. Servlets is almost always more efficient than CGIs because your ' re not starting up a new operating-system-level process EV Ery time Somebody clicks on a link or button.
Tomcat includes an optional CGI servlet This allows you to run legacy CGI scripts; The assumption is, and the most new back-end processing would be do by user-defined Servlets and JSPs.
To enable Tomcat's CGI servlet, you must does the following:rename the file Servlets-cgi.renametojar (found in Catalina_hom e/server/lib/) to Servlets-cgi.jar, so and the servlet that processes CGI scripts would be on Tomcat ' s CLASSPATH. In Tomcat's Catalina_base/conf/web.xml file, uncomment the definition of the servlet named CGI (this was 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 are a safe place to hide things that yo U don ' t want the user to being 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 would not Being the same as the URL in the previous step. Restart Tomcat, and your CGI processing should now is operational. The default directory for the servlet to locate the actual scripts is web-inf/cgi. As have been noted, the Web-inf directory is protected against casual snooping from browsers, so this was a good place to PU T CGI scripts, which may contain passwords or other sensitive information. For compatibility and other servers, though, the keep in the scripts directory, traditional, B, prefer to/cgi-bin UT be aware, that files in this directory is viewable by the curious web surfer. Also, on Unix, being sure that the CGI script files is executable by the user under which you is running Tomcat.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.