Feb 15

PHP ScrewsSometimes, you want to run PHP with Tomcat. Why? Well, you may have a legacy product, for instance, that will require servlets for many more years. Or you may be using this gigantic Java program and are only interested in adding a tiny PHP piece on the side.

There are many guides showing how to do this available, but they become outdated almost as soon as they are published. So, it’s my turn to write a short-lived guide, this time for PHP 5.2.5 :)
Note thas this post relies greatly on information found here. Too bad even that guide got old so fast!

  1. Go to http://www.php.net/downloads.php and download the current version. I am going to do the setup on a Windows machine here, so I can simply download the binaries. On *nix, you will need to compile PHP. I know I will have to, anyway…
  2. You also need to download the corresponding PECL modules.
  3. Let’s assume that your current Tomcat install can be found in c:\Tomcat5\. Create a c:\Tomcat5\php\ directory and unzip the PHP zip file in it.
  4. Rename php.ini-dist, in c:\Tomcat5\php\, to php.ini
  5. Extract php5servlet.dll from the PECL zip file to c:\Tomcat5\php\
  6. Create a directory under c:\Tomcat5\webapps\; in our case: phptest
  7. In c:\Tomcat5\webapps\phptest\, create a subdirectory: WEB-INF
  8. In c:\Tomcat5\webapps\phptest\WEB-INF\, create web.xml with the following content:
    ini
    1. <?xml version="1.0" encoding="ISO-8859-1"?>
    2. <!DOCTYPE web-app PUBLIC
    3.   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    4.   "http://java.sun.com/dtd/web-app_2_3.dtd">
    5. <web-app>
    6.         <servlet>
    7.                 <servlet-name>php</servlet-name>
    8.                 <servlet-class>net.php.servlet</servlet-class>
    9.         </servlet>
    10.         <servlet>
    11.                 <servlet-name>php-formatter</servlet-name>
    12.                 <servlet-class>net.php.formatter</servlet-class>
    13.         </servlet>
    14.         <servlet-mapping>
    15.                 <servlet-name>php</servlet-name>
    16.                 <url-pattern>*.php</url-pattern>
    17.         </servlet-mapping>
    18.         <servlet-mapping>
    19.                 <servlet-name>php-formatter</servlet-name>
    20.                 <url-pattern>*.phps</url-pattern>
    21.         </servlet-mapping>
    22. </web-app>
  9. Extract/unjar (using jar xvf or WinZip) php5srvlt.jar under c:\Tomcat5\php\tmp\
  10. Modify both c:\Tomcat5\php\tmp\net\reflect.properties and c:\Tomcat5\php\tmp\net\servlet.properties, replacing
    ini
    1. library=phpsrvlt

    with

    ini
    1. library=php5servlet

    and save.

  11. Jar the content of c:\Tomcat5\php\tmp\ into a new version of php5srvlt.jar
  12. Move php5srvlt.jar to c:\Tomcat5\common\lib\
  13. Copy c:\Tomcat5\php\php5servlet.dll and c:\Tomcat5\php\php5ts.dll to c:\windows\system32\
  14. Create a test page in c:\Tomcat5\webapps\phptest\test.php with this contents:
    PHP
    1. <?php phpinfo(); ?>
  15. Start Tomcat and go to http://localhost:8080/phptest/test.php

It should work. If it doesn’t, you can always post the stack trace here.

Sphere: Related Content

Feb 03

References…almost :)
Another update:
After reading some of the comments to this entry, I googled ‘pass-by-reference’ and discovered that the semantic aspect of what the phrase means has been the topic of many topics along time with funny titles such as “More on Java- pass by reference, but not really” or more seriously “Parameter passing in Java - by reference or by value?” or what I consider the clearest, best articulated article on the topic: Java is Pass-By-Value, Dammit!
(end of update)

Next day update:
OK, thanks go to the first 3 commenters: when venturing into the semantic field, this entry is inaccurate. I should have used this title actually: Java Does Pass-A-Reference because it actually passes the reference’s value.
What does this mean? Well, Java passes a reference to the original object; however it passes a copy of its original reference.
After making this clear, you can keep reading because the point I am making - which is that the language’s behaviour, similar to C’s behaviour, should not come as a surprise - still stands and I am glad to be prompted to clarify it since it makes easier for the reader to grasp why modifying the argument does not change the reference :)
(end of update)

It is simple: either a function/method argument is an object, in which case a reference is passed, or it is not and the argument’s value is passed.
I just read this blog entry that somehow made it to DZone’s home page: Java Does Not Pass-By-Reference.
Well, it is unfortunate. The author makes the case that Java does not pass a reference to an object in a way that would allow him to manipulate said reference.
Of course, this is correct but I find the examples provided somewhat misguided since references themselves are not here to be manipulated but to let you manipulate the object (or, since he mentions C, the memory space) they reference.

Of course, to stay in tune with his C analogy, I should mention that you can modify the reference’s value itself if you use a pointer to a pointer in your function/method signature. For instance, in C, to modify a char* reference, this would look like this:

void myfunction(char **my_double_dereference)
{
    (*mydoublereference) = (char*)malloc(sizeof(char*)*my_size);
}

Now, our original memory space was not modified in the process. However, its reference was lost and replaced with a new reference to a new memory allocation.

C was originally written to be a better macro-assembler. With each new language generation, we are getting further away from the original intent by abstracting the architecture of the machine your program is compiled for. Java is one of the “recent” (Ahem! It’s all relative) languages that tried to free developers from having to worry about pointers. Of course, it quickly became obvious that if, on the one hand, it was easy to write a new program in Java, memory management was not going away and it is very difficult to write clean code if you do not understand its principles. (Note to some commenters: I never wrote that Java does away with pointers)

Note that if you wish to modify references in Java, you can still do that, but you have to be a bit more creative and wrap your reference in another object: this is, for example, how weak references work as you are actually passing a reference to the weak reference’s constructor. Not the object referenced.

Using the same model, let’s rewrite Adam’s Figure 4:

public static void main(String[] args) {
    StringBuffer sb1 = new StringBuffer("Hello");
    StringBufferWrapper wrapper = new StringBufferWrapper(sb1);
    doSomething(wrapper);
    sb1 = wrapper.reference;   

    // what gets printed?
    if (sb1 == null) {
        System.out.println("sb1 is null");
    } else {
        System.out.println("sb1 is not null");
    }
}   

public void doSomething(StringBufferWrapper sb2) {
    sb2.reference = null;
}

I hope I didn’t introduce any mistake in my code, it’s a tad late but you get the idea.

As usual, let me know if this elicits any question on this topic or weak references or what not…
Cheers!

Sphere: Related Content