A detailed explanation of PHP and Java Integrated Development (i)

Source: Internet
Author: User
Tags install php php class

A long time ago, someone from the WWW to see the sky on a bright bright spot, 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.


An example of a Java class in a PHP script

Now that you know the basics of Php/java Bridge, you can start to develop your first Php/java application, here is a simple example, it is a PHP script, implemented +,-,*,/four basic operations of the calculator, Java will be the core engine of the application , because the result of the calculation is provided by a Java class called Calculatorbean, in other words, PHP will pass the operand to the Java function, then receive the result of the calculation and display the result to the user.

The Calculatorbean class defines four functions for the operator, defines a function for the returned result, and the complete code is as follows

java.io.Serializable {private float result; public Calculatorbean () {this.result = 0.0f;} public float Addab (float A, float b)   {this.result = a+b; return result;} public float Subab (float A, float b)   {T His.result = A-B; return result; public float MplAB (float A, float b)   {this.result = a*b; return result;} public float Divab (float  A, float b)   
return result;public float GetResult () {   return this.result;   }}

When you compile this Java source code, the result is a. jar file called Calculator.jar that copies the file to the Appname/web-inf/lib directory.

You can then write a PHP script to invoke the previous Java class, using the function described previously, you can write a calculator.php file, reference

Put calculator.php in the appname directory if you are testing under the Tomcat server

(localhost:8080), in the address bar of the browser, enter:

http://localhost:8080/appName/calculator.php, Enter, you should see the page shown in 1:

Examples of PHP scripts in JSP pages

If you want to use PHP's random function to get a random number between 0 and Max, you can use ScriptEngine as follows.

<%  javax.script.ScriptEngine e =   Php.java.script.EngineFactory.getInvocablePhpScriptEngine (this, Application, request, response);  E.getcontext (). Setwriter (out);  
{return rand (0,java_values ($maxim));}; ? ");  Javax.script.Invocable i = (javax.script.Invocable) e;  Object result = null;  
New Object[]{new Integer (100)});  
Flush the output generated by invokexxx  out.println ("Random number printed from the servlet:" + result);%>

In the browser address bar, enter:http://localhost:8080/appName/random.jsp, enter, and you may see a result like this:

PHP scripts in Java desktop applications

This task is easily implemented using the Javax.script.ScriptEngineManager and Javax.script.ScriptEngine classes, http://javaboutique.internet.com/ Tutorials/thebridge/listing3.html shows a j2se/php application with the same functionality as before, and you can test the application at a command prompt:

? Gt;java–classpath {Place here the application Classpath} javaphp

Running PHP scripts with the Jrunscript tool

Java comes with a tool called Jrunscript that can be used to assign values to PHP scripts from Java, such as:

jrunscript-l php {phpfile}.php

As in the following hello.php script:

      

You can run jrunscript like this:

.. > jrunscript-l php hello.php

To work better

This article only made a brief introduction to the Php/java Bridge, hoping to help you, Php/java Bridge in PHP and Java between the two high-level programming language to build an interoperability bridge, if you think this technology is helpful to your project, then do not hesitate to do it!

Reference Address: http://www.chinaz.com/program/2008/1113/44836.shtml

A detailed explanation of PHP and Java Integrated Development (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.