PHP Doku:: Beispiele - java.examples.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DienstePHP / Java IntegrationBeispiele

Ein Service von Reinhard Neidl - Webprogrammierung.

PHP / Java Integration

<<Java Servlet SAPI

Java Beispiel>>


UnterSeiten:

Beispiele

Inhaltsverzeichnis


2 BenutzerBeiträge:
- Beiträge aktualisieren...
anto dot justus at gmail dot com
10.06.2009 19:18
Java - php class

Third-party classes are used within a PHP application,
 I'll create a simple Java class that calculates
sales tax based upon a price and taxation rate
input by the user.

You'll need to compile salesTax.java using
the Java compiler before you can use this class
within the PHP script

import java.util.*;
import java.text.*;

public class SalesTax {

public String SalesTax(double price, double salesTax)
{

    double tax = price * salesTax;

    NumberFormat numberFormatter;

    numberFormatter = NumberFormat.getCurrencyInstance();

    String priceOut = numberFormatter.format(price);

    String taxOut = numberFormatter.format(tax);

    numberFormatter = NumberFormat.getPercentInstance();

    String salesTaxOut =
numberFormatter.format(salesTax);

    String str = "A sales Tax of " + salesTaxOut +

                 " on " + priceOut + " equals " + taxOut + ".";

    return str;

    }

}

file named salesTax.java, you'll need to compile it.

a new, compiled file named salesTax.class
 will be created.

This is the compiled code that will be called by
PHP to perform the calculation based on the data
 input by the user via an HTML form.

salesTaxInterface.php

<?php

// Format the HTML form.

$salesTaxForm = <<<SalesTaxForm

<form action="SalesTaxInterface.php" method="post">

   Price (ex. 42.56):<br>

   <input type="text" name="price" size="15" maxlength="15" value=""><br>

   Sales Tax rate (ex. 0.06):<br>

   <input type="text" name="tax" size="15" maxlength="15" value=""><br>

   <input type="submit" name="submit"
value="Calculate!">

   </form>

SalesTaxForm;

if (! isset(
$submit)) :

   echo
$salesTaxForm;

else :

  
// Instantiate the SalesTax class.
  
$salesTax = new Java("SalesTax");

  
// Don't forget to typecast in order to

   // conform with the Java method specifications.

  
$price = (double) $price;
  
$tax = (double) $tax;

   print
$salesTax->SalesTax($price, $tax);

endif;

?>

Troubleshooting

Chances are you will encounter various
minor problems when you first attempt to integrate
Java and PHP functionality, particularly if you are a
relative newcomer to the Java programming
environment. Even if the Java code compiles
correctly, you may still encounter problems,
largely due to differences found between the
two languages.

two of these differences here:

Data types

PHP is a loosely-typed language, which means it is
 rather lenient on the way variables are used.
On the contrary, Java is a strongly-typed language,
 which means that its policies for handling
variables and data types are rather stringent.
To illustrate the problem this difference poses,
take a moment to again review the code.
 Notice that I had to typecast the $price and
$tax variables before passing them to the
SalesTax() method, because this
method requires that both input parameters
 are of type "double." If this is not done, then
 input such as 24 for price would cause an
 error to occur.

Furthermore, if you are not adamant in
ensuring the correct data types are passed
to the Java methods, you may receive
unexpected results, although it will not be
outwardly apparent that an error has occurred.
 Therefore, be careful!

Error reporting

Errors occurring within a PHP script are reported
 in accordance with the level of error-reporting
 specified in the php.ini file. Because the
Java code is called from within the PHP script,
any errors that arise from the Java code are
displayed as PHP errors. If you would like to
 prevent these errors from being displayed
to the browser, simply prefix a @ symbol to
 the PHP command.
anto dot justus at gmail dot com
10.06.2009 18:01
PHP JavaBridge to work with PHP5 -  windows server:

steps:

1- Install Java J2EE 1.5 + JDK 1.4 (which includes application server/deploy tool/etc...)
2- download pecl-5.0.5-Win32.zip and php-java-bridge_2.0.8.zip, which will include
extra dll(s)
   - unpack pecl pkg to your extensions folder, in PHP5 its ext.
   - unpack java-Bridge to root php folder, in my case its simply C:\PHP
   Note: the java-Bridge inculdes new versions of certain files like php_java.dll
   so, it would be wise to rename your old files that came with PECL pkg for example
   file_old, to rollback at anytime.

In order to deploy/test Java-Bridge .war onto your java application server follow these steps

http://cvs.sourceforge.net/viewcvs.py/php-java-bridge/php-java-bridge/
INSTALL.WINDOWS?view=markup

Note: move JavaBridge.jar to your extensions folder. and in test.php file that came
with Java-Brdige package change line java_require("test/arrayToString.jar");
to java_require("tests.php4/arrayToString.jar");

Add the following to your php.ini file and restart server:-
java.classpath = "location of JavaBridge.jar file...i.e. to your PHP extensions folder\
JavaBridge.jar,also any other extra java files that you'll be instanciating using your php script"
java.java_home = "location of jdk\bin"
java.libpath = "location of php_java.dll file...i.e. also to your PHP extensions folder"



This example uses the PHP/Java Bridge to connect to a SOAP service.
////////////////////////////////////////////////////////////////////

#!/bin/env php
<?php

 

require_once("java/Java.inc");

include(
"wsimport.php");

try {
 
$addNumbersService = new java("org.duke.AddNumbersService");
 
$port = $addNumbersService->getAddNumbersPort();
 
$number1 = 10;
 
$number2 = 20;
  echo (
"Invoking one-way operation. Nothing is returned from service.\n");
 
$port->oneWayInt($number1);
  echo (
"Invoking addNumbers($number1, $number2)\n");
 
$result = $port->addNumbers($number1, $number2);
  echo (
"The result of adding $number1 and $number2 is $result\n\n");
 
$number1 = -10;
  echo (
"Invoking addNumbers($number1, $number2)\n");
 
$result = $port->addNumbers($number1, $number2);
  echo (
"The result of adding $number1 and $number2 is $result\n\n");
} catch (
JavaException $ex) {
 
$ex = $ex->getCause();
  if (
java_instanceof($ex, java("org.duke.AddNumbersFault_Exception"))) {
   
$info = $ex->getFaultInfo()->getFaultInfo ();
    echo (
"Caught AddNumbersFault_Exception: $ex, INFO: $info.\n");
  } else {
    echo (
"Exception occured: $ex\n");
  }
}
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)