PHP and Java are combined with PHP and Java for integrated development (1 ). [Php100 exclusive article] a long time ago, someone saw a bright highlight in the sky from www. it was the Java language. at the same time, in another place, a dreamer also saw a [php100 exclusive article] a long time ago, someone saw a bright highlight on www, which is the Java language. at the same time, in another place, a dreamer also saw a bright spot, namely PHP.
As time passes, the two highlights become increasingly bright and soon become popular with programmers. some people have doubts: what will happen if they encounter the two? Is it possible to combine their strengths?
Try to build a bridge between PHP and Java, and use this bridge to establish a communication channel between the two entities. with the help of this bridge, you can develop classes in Java, then they are called in PHP. Similarly, PHP scripts can be used in your Java desktop or Web applications.
In this article, you will learn how:
◆ Install and configure the PHP/Java Bridge
◆ Use Java classes in PHP scripts
◆ Use PHP scripts in Java classes
◆ Use PHP scripts on the JSP page
Let's get started!
Install and configure the PHP/Java Bridge
The latest PHP/Java bridge zip package can be found.
◆ For J2SE, the installation is very simple:
◆ Install J2SE 1.6 or later
◆ Install PHP 5.1.4 or later
Decompress the php-java-bridge_5.2.2_j2ee.zip package
Enter the directory after decompression from the command prompt, and enter:
?>java ?classpath JavaBridge.war TestInstallation |
In this folder, you should see an ext directory, which contains four. jar file, copy the JavaBridge. jar and php-script.jar to the ext folder under your J2SE installation directory (usually {JAVA_HOME}/jre/lib/ext ).
For J2EE, perform the following installation steps:
Copy the JavaBridge. war file to the auto_deploy folder under your J2EE Server or servlet engine (Tomcat, Resin, etc.
Rename the file according to your application, restart the J2EE Server, and wait for the automatic deployment process to create and. war file directory. In this example, this application is called appName. war.
Test the new application in a browser and enter:Http: // localhost: 8080/appNameAnd then click test. php.
If your J2EE Server runs on different hosts and ports, modify the parameters accordingly.
Note: If you want to run J2EE/PHP applications on Apache or IIS, copy the directory containing appName to the root directory of Apache/IIS documents.
Use Java classes in PHP scripts
First, you must know which special functions (PHP class) PHP/Java bridge has created for integrating Java classes in PHP scripts. These functions include:
◆ Java: it allows you to access the Java type of a given name, for example:
◆ Java ("java. lang. System")-> getProperties ();
◆ Java_autoload: it allows you to load a set of Java libraries in the current PHP script, such:
◆ Java_autoload ("my_1.jar; my_2.jar ");
◆ Java_cast: it allows you to convert a Java object into a PHP value, such:
$mystr=new java("java.lang.String","9");$phpnr=java_cast($mystr,"integer");echo $ phpnr; |
◆ Java_is_null: you can use it to check whether the value is null, for example:
◆ Java_is_null (java ("java. lang. System")->; getProperty ("my_prop "))
◆ Java_session: you can use it to return a session handle, for example:
$session = java_session(); |
◆ Java_values: you can use it to evaluate an object and extract its content (only when this operation is possible), such:
$result = java_values($calcinstance->addAB($term_1,$term_2)); |
To use these functions, your PHP application must contain the corresponding PHP class. The most important class is Java. inc, which has a complete list of classes under the Directory appName/java. inc, you can get Java class instances, such:
$calcinstance=new Java("calculator.CalculatorBean"); |
Reminder: there are more functions and their details under the installation directory/documentation/API of PHP/Java bridge. Use Java classes in PHP scripts
Use PHP scripts in Java classes
To call the PHP method in a Java application, you must be familiar with the Java API. The most important class of this API 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 the expected full functions of every Java ScriptEngine. it is implemented through such functions as AbstractScriptEngine, InteractivePhpScriptEngine, InvocablePhpScriptEngine, callback, PhpScriptEngine, and PhpServletScriptEngine.
Bytes
Javax. script. Invocable: this interface provides a function that allows java applications to call intermediate layer code under the script compiler.
Reminder: more classes and their details are available in the/documentation/server/documentation/API directory.
1
...