herberlin.de tools.herberlin.de tools.herberlin.de

PhpServlet

Introduction

If you are developing JSPs and servlets you might sometimes find some old php scripts and projects that you probably would like to run on your server. But the servlet engine does not work with php, so you have to download and install Apache, configure php, configure the servlet engine connector or worse use different ports and so on. It would be nice to get php running on Tomcat or any other servlet engine, just for development purposes.

As you probably know a servlet comes with php that should solve this problem. But it does not. With the current php version (4.3.2), and maybe other versions too, this servlet does not work. While lots of environment variables - the whole windows environment stuff - are given to php, the HTTP_ variables are completely missing. Even worse, a "404 Not Found" header from php crashes Tomcat (4.1.19). If you want to try this servlet, an introduction can be found here.

The Herberlin php servlet is a pure Java implementation that does not use native libraries. It's easy to install and configure (like any other servlet). It calls the php interpreter as a process according to the specifications of the Common Gateway Interface (CGI) Version 1.1. This is a well-documented open standard that is also supported by the Apache web server, although in a production environment the use of the php machine as Apache module is recommended. Anyway, for a development environment, the cgi mode is fast enough.  

Licence

Herberlin phpservlet and the bundled herberlin wwwutil - library are delivered under the terms of the GNU LESSER GENERAL PUBLIC LICENSE and copyrighted by Hans Joachim Herbertz http://www.herberlin.de. You should find the licence text with the distribution.

Running

Software and documentation are packed as a web application war-file. Download the current release
and drop it into the webapp-directory of your servlet engine. Call http://localhost/phpservlet and see this page on your local environment.

The servlet engine will unpack the war-file into a directory named phpservlet. Find the file WEB-INF/web.xml and set the path to your php-executable as described there. A sample web.xml can be found here.

The following links only work on your local server:
If you want to compile the PhpServlet you'll need the sources and the WEB-INF/lib/herberlin_wwwutil.jar library that also comes with sources included in the jar-file. For a simple deployment onto your server I packed the PhpServlet and the library together into one jar phpservlet.jar. For servlet configuration see WEB-INF/web.xml.

Limitations / Features

The phpservlet requires a Java runtime version 1.4. The multipart-form-data encoding (file upload) is now supported. Configurable cgi - environment via web.xml. Known bugs and version history can be found here.

Tests

The following links only work on your local server:
Find the following php-files for your tests:
  • phpinfo.php displays the phpinfo.
  • globals.php shows php environment and server variables. You may also do some requests with querystring and post variables.
  • redirect.php sends a Location redirect header.
  • include.php includes the deployment descriptor to test the include.
  • upload.php tests the multipart-formdata (file upload). You must enable php.use.multipart.formdata in the web.xml before running this test.
  • error.php leads to a php error for an include file is not present.
Among the sources some unit tests for development purpose can be found. To run the tests you will need the JUnit and HttpUnit frameworks.

Feedback

The phpservlet is not yet well tested. As mentioned above I'm running it on Windows XP and Tomcat 4.1.29 with php 4.3.2, Win2000, Caucho Resin 2.1.4 and php 4.2.2 and Tomcat 5.0.16 with php 4.1.0 on Suse Linux 8. Both pass the tests and seem to work. If you find any bugs with these or other configurations please report to phpservletATherberlin.de.

Thanks and have fun.

Hans Joachim Herbertz

http://tools.herberlin.de/phpservlet/index.shtml
Last Modified: 2010-12-09
© 2004 Hans Joachim Herbertz