home forums resources search newsjoinmembers: 6056
Hiveminds Network: Joomla Wordpress Drupal Fireorb Flash Java PHP Ruby Windows Linux
Hiveminds | Wed, 2008-06-25 14:53  tags: , ,

Want to run PHP and access Java? Here's how to do it on Windows XP using Apace and Apache Tomcat. Apache Tomcat is a Servlet container developed at the Apache Software Foundation (ASF). Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications from Sun Microsystems, and provides a "pure Java" HTTP web server environment for Java code to run. Tomcat should not be confused with the Apache web server, which is a C implementation of a HTTP web server; these two HTTP web servers are not bundled together. Apache Tomcat includes tools for configuration and management, but can also be configured by editing configuration files that are normally XML-formatted.

Try it using XAMPP

Although the tutorial show you how to do things by installing Apache Tomcat, this may not be ideal or what most want to do. One of the things that stopped me from digging into Java web development is the fact that setting up an environment on Windows is just as hard as finding a cheap Java web hosting company. That and the fact that I like to be able to turn my development environments on and off. But now thanks to apachefriends.org half of my problem is solved. They have released an Apache Windows XAMPPpackage with an easy to install Apache Tomcat add-on. I have been waiting a long time for this to happen. This event should make things even easier for beginners.

Installation Directories

  • C:\PHP
  • C:\Program Files\Apache Group\Tomcat 5.5
  • C:\Program Files\Java\jdk1.5.0_10

Java

  1. Make sure you set Environment Variables for Java (CLASSPATH and PATH). It looks like;

    CLASSPATH = .
    JAVA_HOME = C:\Program Files\Java\jdk1.5.0_10
    PATH = %PATH%;%JAVA_HOME%\bin
    

    JAVA CLASSPATH

Apache Tomcat Server

  1. Run apache-tomcat-5.5.20.exe. Click Next >.
  2. Click I Agree.
  3. Select "Full" from the dropdown, and then click Next >.
  4. Click Next >. (in my case, C:\Program Files\Apache Group\Tomcat 5.5)
  5. Enter an admin password, and then click Next >.
  6. Verify the J2SE 5.0 JRE installation path is correct, and then click Install. (in my case, C:\Program Files\Java\jre1.5.0_10)
  7. click Finish.
  8. Open http://localhost:8080/ in your web browser. If you see something like the following, you have properly installed Tomcat!

    Tomcat Server

  9. To install PHP as FastCGI, add these lines (from c:\php-java-bridge-3.2.1_j2ee\WEB-INF\web.xml)
    to the end of your web.xml file (C:\Program Files\Apache Group\Tomcat 5.5\conf\web.xml):

    ...
    <!-- From c:\php-java-bridge-3.2.1_j2ee\WEB-INF\web.xml -->
    
        <!-- Faces Servlet -->
        <servlet>
            <servlet-name>PhpFacesServlet</servlet-name>
    
            <servlet-class>php.java.faces.FacesServlet</servlet-class>
            <load-on-startup>0</load-on-startup>
        </servlet>
    
        <!-- PHP Servlet: back-end for Apache or IIS -->
    
        <servlet>
            <servlet-name>PhpJavaServlet</servlet-name>
            <servlet-class>php.java.servlet.PhpJavaServlet</servlet-class>
    
             <!-- Allow non-local clients. These clients connect to the -->
    
             <!-- back-end using a HTTP tunnel. Default is Off. -->
             <!-- Leave this off if your server is connected to the internet. --> 
           <!--
             <init-param>
               <param-name>allow_http_tunnel</param-name>
               <param-value>On</param-value>
    
             </init-param>
            -->
    
             <!-- Default it the java.log_level from the php.ini. -->
             <!-- See also the system property php.java.bridge.default_log_level -->
            <!--
             <init-param>
    
               <param-name>servlet_log_level</param-name>
               <param-value>2</param-value>
             </init-param>
            -->
    
            <load-on-startup>0</load-on-startup>
      </servlet>
    
        <!-- PHP CGI servlet: when IIS or Apache are not available -->
        <servlet>
            <servlet-name>PhpCGIServlet</servlet-name>
    
            <servlet-class>php.java.servlet.PhpCGIServlet</servlet-class>
    
             <!-- Allow us to override the java.hosts, java.servlet and -->
             <!-- java.socketname settings from your php.ini so that we can -->
             <!-- use the current VM (see X_JAVABRIDGE_OVERRIDE_HOSTS). -->
             <!-- Default is On. -->
    
            <!-- 
             <init-param>
               <param-name>override_hosts</param-name>
               <param-value>Off</param-value>
             </init-param>
    
            -->
    
             <!-- Your php binary. Default is /usr/bin/php-cgi or -->
             <!-- c:/php/php-cgi.exe -->
            <!-- 
             <init-param>
               <param-name>php_exec</param-name>
    
               <param-value>php-cgi</param-value>
             </init-param>
            -->
    
             <!-- The number of concurrent cgi requests -->
             <!-- Note that this value should be 1/2 of the servlet engine's -->
    
             <!-- thread pool size because each PhpCGIServlet instance also -->
             <!-- consumes one PhpJavaServlet instance -->
            <!-- 
             <init-param>
               <param-name>max_requests</param-name>
               <param-value>50</param-value>
    
             </init-param>
            -->
    
             <!-- Use the fast cgi interface instead of cgi when running -->
             <!-- php as a CGI sub component (when php is not installed as an -->
             <!-- Apache or IIS module). Values: Off, On, Autostart. -->
             <!-- Default is Autostart. -->
    
            <!-- 
             <init-param>
               <param-name>use_fast_cgi</param-name>
               <param-value>Off</param-value>
             </init-param>
    
            -->
    
             <!-- Share the FastCGI pool from the JavaBridge PhpCGIServlet with -->
             <!-- the GlobalPhpCGIServlet used by all other web apps. -->
             <!-- Set this to On in both, the JavaBridge/WEB-INF/web.xml and -->
             <!-- in the global conf/web.xml. -->
             <!-- Default is Off. -->
    
             <!-- See http://php-java-bridge.sf.net#global-servlet for details. -->
             
             <init-param>
               <param-name>shared_fast_cgi_pool</param-name>
               <param-value>On</param-value>
             </init-param>
    
            
    
            <load-on-startup>0</load-on-startup>
      </servlet>
    
        <!-- Faces Servlet Mapping -->
        <servlet-mapping>
            <servlet-name>PhpFacesServlet</servlet-name>
    
            <url-pattern>*.jsf</url-pattern>
        </servlet-mapping>
    
        <!-- PHP Servlet Mapping -->
        <servlet-mapping>
            <servlet-name>PhpJavaServlet</servlet-name>
    
            <url-pattern>*.phpjavabridge</url-pattern>
        </servlet-mapping>
    
        <!-- PHP CGI Servlet Mapping -->
        <servlet-mapping>
            <servlet-name>PhpCGIServlet</servlet-name>
    
            <url-pattern>*.php</url-pattern>
        </servlet-mapping>
    
        <!-- Welcome files -->
        <welcome-file-list>
            <welcome-file>index.php</welcome-file>
    
        </welcome-file-list>
    ...
    </web-app>
    
  10. Copy JavaBridge.jar and php-servlet.jar from C:\php-java-bridge-3.2.1_j2ee\WEB-INF\lib to C:\Program Files\Apache Group\Tomcat 5.5\shared\lib.

    PHP5JSP 1

    PHP5JSP 2

PHP 5 & Java Bridge

  1. You may use some sort of tools to unzip the php-java-bridge_3.2.1_j2ee.zip
    file. I used ALZip.
  2. Copy JavaBridge.jar from the JavaBridge.war to C:\PHP\ext.

    JavaBridge.jar

  3. Copy java-x86-windows.dll from the JavaBridge.war to C:\PHP\ext.

    java-x86-windows.dll

  4. Rename java-x86-windows.dll (from C:\PHP\ext) to php_java.dll

    rename java-x86-windows.dll

  5. To set up a valid configuration file for PHP, make a copy of
    php.ini-recommended then rename it to the php.ini.

    PHP INI

  6. Edit the php.ini so that the
    "extension_dir" points to your PHP 5 extension directory.

      
    ...
    
    extension_dir = c:\php\ext
    
    extension = php_java.dll
    
    ...
    

    PHP exe 1

    PHP exe 2

  7. Add Java module settings to php.ini.
    ;;;;;;;;;;;;;;;;;;;
    ; Module Settings ;
    ;;;;;;;;;;;;;;;;;;;
    
    [java]
    java.java_home = "C:\Program Files\Java\jdk1.5.0_10\bin"
    java.java = "C:\Program Files\Java\jdk1.5.0_10\bin\javaw.exe"
    java.class.path = "C:\php\ext\JavaBridge.jar"
    java.library.path = "C:\php\ext"
    java.hosts = "127.0.0.1:8080"
    java.servlet = On
    java.log_level = 2
    

Test & Examples

  1. Open Notepad, and type (phpinfo.php)


    <?php
    
    phpinfo(); 
    
    ?>
    
  2. Save it as phpinfo.php into C:\Program Files\Apache Group\Tomcat 5.5\webapps\test.
  3. Open http://localhost:8080/test/phpinfo.php in your web browser.

    phpinfo() 1

    phpinfo() 2

  4. Example - java1.php
    <?php
    
    // get instance of Java class java.lang.System in PHP
    $system = new Java('java.lang.System');
    
    // demonstrate property access
    echo 'Java version=' . $system->getProperty('java.version') . '<br/>';
    echo 'Java vendor=' . $system->getProperty('java.vendor') . '<br/>';
    echo 'OS=' . $system->getProperty('os.name') . ' ' .
                 $system->getProperty('os.version') . ' on ' .
                 $system->getProperty('os.arch') . ' <br/>';
    
    // java.util.Date example
    $formatter = new Java('java.text.SimpleDateFormat',
                         "EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");
    
    echo $formatter->format(new Java('java.util.Date'));
    
    ?>
    
    

    Java Info.

  5. Example - HelloWorld.java
    public class HelloWorld {
    	String hw = "Hello World";
    
    	public String getHelloWorld() {
    		return hw;
    	}
    }
    
    
  6. To compile a java source file, type "javac HelloWorld.java"
  7. To create a JAR file from the class file, type "jar cvf HelloWorld.jar
    HelloWorld.class".

    Java CLASS

  8. Copy the HelloWorld.jar file into the C:\Program Files\Apache Group\Tomcat 5.5\webapps\test directory.

    Java CLASS 1

  9. Code the helloworld.php file.
    <?php
    
    java_require('http://localhost:8080/test/HelloWorld.jar');
    $myObj = new Java('HelloWorld');
    
    // display Hello World
    echo (String) $myObj->getHelloWorld();
    
    ?>
    
  10. Open http://localhost:8080/test/helloworld.php in your web browser.

    Java CLASS 2

Sample Files

  1. config files (web.xml & php.ini)

References

  1. PHP/Java Bridge - INSTALL.WINDOWS

  2. Happy Publishing!

    Hiveminds's picture
    This article brought to you by the Hiveminds Magazine - Staff. Contact us if you want to post an article or announcement anonymously
 
Bitrix Site manager - fast to create, easy to manage CMS Comparison Matrix
Put Your Site Here Developer Links
Web Developers Adobe Flex Content Management Systems Joomla! Drupal Wordpress ASP.NET Silverlight Windows
 

Newsletter

Get updates on Hiveminds services, articles and downloads by signing up for the newsletter.

Editor's choice

Some of the better articles, stories and tutorials found at Hiveminds.

Find more

Find more of Hiveminds articles, stories, tutorials and user comments by searching.




Picked links

Hand picked websites and articles from around the web that provide quality reading.