"Php100 exclusive feature" A long time ago, someone from the www saw a very bright bright spot in the sky, it is the Java language, at the same time, in another place a dreamer also saw a bright spot, it is PHP.
As time went by, the two bright spots became brighter, and soon they were liked by programmers, so there was a question: what would happen if they met? Is it possible to combine their strengths?
Try to build a bridge between PHP and Java, using this bridge to build a communication channel between the two entities, with the help of this bridge, you can develop classes in Java and then invoke their methods in PHP, as well, You can also use PHP scripts in your Java desktop or Web application.
In this article, you will learn how to:
Installing and configuring the Php/java Bridge
Using Java classes in PHP scripts
Using PHP scripts in Java classes
Using PHP scripts in JSP pages
So let's get started!
Installing and configuring the Php/java Bridge
The latest Php/java Bridge ZIP package is available for download in http://sourceforge.net/projects/php-java-bridge/, and the installation process relies on choosing which Java platform to interact with the PHP script through the bridge.
For J2SE, installation is simple:
Installing J2SE 1.6 or later
Install PHP 5.1.4 or later
Unzip the Php-java-bridge_5.2.2_j2ee.zip bag
From the command prompt, enter the directory you just unzipped, and enter:
>java? classpath Javabridge.war testinstallation |
Under this folder, you should see an ext directory, which includes four. jar files, and copies the Javabridge.jar and Php-script.jar to the Ext folder in your J2SE installation directory (usually {java_home}/jre/ Lib/ext).
For the Java EE, perform the following installation steps:
Copy the Javabridge.war file to the Auto_deploy folder under your Java EE server or the servlet engine (tomcat,resin, etc.).
Rename the file according to your application, then restart the Java EE Server and wait for the automatic deployment process to create the directory associated with the. war file, in this case the application is called Appname.war.
Test the new application from the browser, enter:http://localhost:8080/appName, and then click Test.php.
If your Java EE server is running on a different host and port, the parameters should be modified accordingly.
Note: If you want to run the j2ee/php application on Apache or IIS, copy the directory that includes the appname to the Apache/iis document root directory.
Using Java classes in PHP scripts
First, you have to know what special functions (PHP classes) are created by the Php/java Bridge to integrate Java classes in PHP scripts, including:
Java: It allows you to access the Java type of a given name, such as:
Java ("Java.lang.System")->getproperties ();
Java_autoload: It allows you to load a set of Java libraries into your current PHP script, such as:
Java_autoload ("My_1.jar;my_2.jar");
Java_cast: It allows you to convert a Java object into a PHP value, such as:
$mystr =new java ("Java.lang.String", "9"); $phpnr =java_cast ($mystr, "integer"); echo $ phpnr; |
Java_is_null: You can use it to check if the value is empty, such as:
Java_is_null (Java ("Java.lang.System")->;getproperty ("My_prop"))
Java_session: You can use it to return a session handle, such as:
$session = Java_session (); |
Java_values: You can use it to evaluate objects and extract their contents (only when this action is possible), such as:
$result = Java_values ($calcinstance->addab ($term _1, $term _2)); |
In order to use these functions, your PHP application must contain the corresponding PHP class, the most important class is Java.inc, under the directory Appname/java has a complete list of classes, through Java.inc, you can get instances of Java classes, such as:
$calcinstance =new Java ("calculator. Calculatorbean "); |
Reminder: There are more functions and their details under the installation directory/documentation/api of the Php/java Bridge. Using Java classes in PHP scripts
Using PHP scripts in Java classes
In order to invoke the PHP method in a Java application, you must be familiar with the Java API, the most important class of which is:
Javax.script.ScriptEngineManager: This class extends the Java.lang.Object class and provides an instantiation mechanism for the ScriptEngine class.
Javax.script.ScriptEngine: This is a Java interface that includes functions that are expected to function in every Java scriptengine, which is done through such as Abstractscriptengine, Interactivephpscriptengine, Invocablephpscriptengine, Invocablephpservletscriptengine, PhpScriptEngine, and Phpservletscriptengine are implemented by these classes.
Javax.script.Invocable: This interface provides a function that allows a Java application to downgrade the middle-tier code in the script compiler.
Reminder: There are more classes and more information in the/DOCUMENTATION/SERVER/DOCUMENTATION/API directory.
1
http://www.bkjia.com/PHPjc/446693.html www.bkjia.com true http://www.bkjia.com/PHPjc/446693.html techarticle "php100 exclusive feature" A long time ago, someone from the www saw the sky a bright bright spot, it is the Java language, at the same time, in another place a dreamer also saw a ...