PHP 5, PHP/Java Bridge & Apache on XP PRO + SP2 and examples

PHP 5, PHP/Java Bridge & Apache on XP PRO + SP2 and examples

Posted : Tue 18/07/2006
Updated: Thu 16/04/2007

Tested Environment

  • php-5.1.6-Win32.zip
  • apache_2.0.59-win32-x86-no_ssl.msi*
  • php-java-bridge_3.2.1_j2ee.zip
  • jdk-1_5_0_10-windows-i586-p.exe
  • Windows XP Pro + SP2

*Add-in modules for Apache 1.3x or 2.0.x are not compatible with Apache2.2.x. If you are running PHP 5.x, you must use the Apache version 2.0.x.

Installed Directories

  • C:\PHP
  • C:\Program Files\Apache Group\Apache2
  • 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
    

Apache HTTP Server

  1. In general, it's simple to install. I installed the Apache HTTP Server forthe localhost (127.0.0.1) on Port 80 (default).

  2. To allow the Apache HTTP Server to by pass Windows Firewall, click Unblock.

  3. To install PHP as an Apache module, add these lines to your Apache2 httpd.conf file (C:\Program Files\ApacheGroup\conf):

    # For PHP 5 do something like this:
    LoadModule php5_module "c:/php/php5apache2.dll"
    AddType application/x-httpd-php .php
    
    # For syntax highlighted .phps files, also add
    AddType application/x-httpd-php-source .phps
    
    # configure the path to php.ini
    PHPIniDir "C:/php"
    

PHP 5 & Java Bridge

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

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

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

  5. To set up a valid configuration file for PHP, make a copy ofphp.ini-recommended then rename it to the 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
    ...
    

Test & Examples

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

    <?php
    
    phpinfo(); 
    
    ?>
    
  2. Save it as phpinfo.php into C:\Program Files\ApacheGroup\Apache2\htdocs\test.
  3. Open http://localhost/test/phpinfo.php in your web browser.

  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'));
    
    ?>
    
    

  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.jarHelloWorld.class".

  8. Copy the HelloWorld.jar file into the C:\Program Files\ApacheGroup\Apache2\htdocs\test directory.

  9. Code the following, and save it as helloworld.php in C:\Program Files\ApacheGroup\Apache2\htdocs\test.
    <?php
    
    java_require('http://localhost/test/HelloWorld.jar');
    $myObj = new Java('HelloWorld');
    
    // display Hello World
    echo (String) $myObj->getHelloWorld();
    
    ?>
    
  10. Open http://localhost/test/helloworld.php in your Web browser.

More Examples

  1. Example - Config.java & config.php

    C:\Program Files\Apache Group\Apache2\htdocs\Config.java.

    public class Config
    {
    	private String dbHost = "myhost";
    	private String dbUser = "myuser";
    	private String dbPass = "mypass";
    	private String dbName = "mydb";
    
    	// constructor
    	public Config()
    	{}
    
    	// constructor
    	public Config(String host, String user, String pass, String name)
    	{
    		dbHost = host;
    		dbUser = user;
    		dbPass = pass;
    		dbName = name;
    	}
    
    	public String getDbHost()
    	{
    		return dbHost;
    	}
    
    	public String getDbUser()
    	{
    		return dbUser;
    	}
    
    	public String getDbPass()
    	{
    		return dbPass;
    	}
    
    	public String getDbName()
    	{
    		return dbName;
    	}
    }
    

    C:\temp\javac Config.java
    C:\temp\jar cvf Config.jar Config.class
    C:\Program Files\Apache Group\Apache2\htdocs\test\Config.jar

    C:\Program Files\Apache Group\Apache2\htdocs\test\config.php.

    <?
    
    java_require('http://localhost/test/Config.jar');
    
    $myObj = new Java("Config");
    
    echo (string) $myObj->getDbHost() . '<br/>';
    echo (string) $myObj->getDbUser() . '<br/>';
    echo (string) $myObj->getDbPass() . '<br/>';
    echo (string) $myObj->getDbName();
    
    ?>
    

  2. Example - Config1.java & config1.php

    C:\Program Files\Apache Group\Apache2\htdocs\test\Config1.java.

    public class Config1
    {
    	private String dbHost = ""; 
    	private String dbUser = ""; 
    	private String dbPass = ""; 
    	private String dbName = "";
    
    	// constructor
    	public Config1()
    	{}
    
    	// constructor
    	Config1(String host, String user, String pass, String name)
    	{
    		dbHost = host;
    		dbUser = user;
    		dbPass = pass;
    		dbName = name;
    	}
    
    	public String getDbHost()
    	{
    		return dbHost;
    	}
    
    	public void setDbHost(String host)
    	{
    		dbHost = host;
    	}
    
    	public String getDbUser()
    	{
    		return dbUser;
    	}
    
    	public void setDbUser(String user)
    	{
    		dbUser = user;
    	}
    
    	public String getDbPass()
    	{
    		return dbPass;
    	}
    
    	public void setDbPass(String pass)
    	{
    		dbPass = pass;
    	}
    
    	public String getDbName()
    	{
    		return dbName;
    	}
    
    	public void setDbName(String name)
    	{
    		dbName = name;
    	}
    }
    
    

    C:\Program Files\Apache Group\Apache2\htdocs\test\config1.php.

    <?php
    
    // config1.php
    
    java_require('http://localhost/test/Config1.jar');
    
    $myObj = new Java("Config1");
    
    $myObj->setDbHost("localhost");
    $myObj->setDbUser("sk33");
    $myObj->setDbPass("abcd");
    $myObj->setDbName("test");
    
    echo (string) $myObj->getDbHost() . '<br/>';
    echo (string) $myObj->getDbUser() . '<br/>';
    echo (string) $myObj->getDbPass() . '<br/>';
    echo (string) $myObj->getDbName();
    
    ?>
    

  3. Example - SalesTax.java, salestax.php, salestax.htm

    C:\Program Files\Apache Group\Apache2\htdocs\test\SalesTax.java.

    import java.util.*;
    import java.text.*;
    
    public class SalesTax
    {
    
        private double tax = 0.0;
        private double price = 0.0;
        private double salesTax = 0.0;
    
        public SalesTax()
        {}
    
        public String SalesTax(double price, double salesTax)
        {
            tax = price * salesTax;
    
            NumberFormat nf = null;
    
            nf = NumberFormat.getCurrencyInstance();
    
            String priceOut = nf.format(price);
            String taxOut = nf.format(tax);
    
            nf = NumberFormat.getPercentInstance();
            String salesTaxOut = nf.format(salesTax);
    
            String str = "Sales Tax of " + salesTaxOut +
                " on " + priceOut + " equals " + taxOut + ".";
    
            return str;
        }
    }
    

    C:\Program Files\Apache Group\Apache2\htdocs\test\salestax.php

    <?php
    
    if (isset($_POST["submit"]))
    {
    	java_require('http://localhost/test/SalesTax.jar');
    
    	$salesTax = new Java("SalesTax");
    
    	$price = (double) $_POST["price"];
    	$tax = (double) $_POST["tax"];
    
    	echo (String) $salesTax->SalesTax($price, $tax);
    }
    
    ?>
    

    C:\Program Files\Apache Group\Apache2\htdocs\test\salestax.htm.

    <html>
    <head>
    <title></title>
    </head>
    <body>
    
    <form action="salestax.php" method="post">
    Price: ($)<br/>
    <input type="text" name="price" size="15" maxlength="15"><br/>
    Tax Rate: (0.1 for 10%)<br/>
    <input type="text" name="tax" size="15" maxlength="15"><br/>
    <input type="submit" name="submit" value="Calculate!">
    </form>
    
    </body>
    </html>
    

Sample Files

  • Config.java
  • config.php
  • Config1.java
  • config1.php
  • HelloWorld.java
  • helloworld.php
  • java1.php
  • php.ini
  • phpinfo.php
  • SalesTax.java
  • SalesTax.php
  • salestax.htm

References

  1. PHP - install.txt
  2. PHP/Java Bridge - INSTALL.WINDOWS & INSTALL.J2EE
  3. PHP4 - Javaintegration on Win32 and examples

Copyright © 2001-2007. Kang, S. Information Systems. University of Wollongong. Australia. All rights reserved.

你可能感兴趣的:(java,PHP,JavaBridge)