Home >Backend Development >PHP Tutorial >Connecting PHP and Java -- PHP/Java Bridge [1]_PHP Tutorial
In the WEB development market, Java and PHP are currently popular technologies. The power of Java is unquestionable. It is not only reflected in WEB development, but also in various software application fields. Java is ubiquitous. PHP is also increasingly prosperous driven by the power of open source and companies such as ZEND, IBM, and Oracle. No one may necessarily believe the prediction of Internet pioneer Marc Andreessen that "PHP will be more popular than Java", but "PHP aims at the enterprise market and competes head-on with Java" is indeed underway. The technology used by 22 million websites around the world makes People have to pay attention.
Two technologies can compete fiercely or work closely together. From the customer's perspective, the integration of technologies is very important, because a huge application system usually cannot be completed independently by a single language/technology. The emergence of PHP/Java Bridge will become a bridge between PHP and Java, providing a good choice for enterprises that need to combine PHP and Java.
//Haohappy does not have a deep understanding of J2EE. If there is any misunderstanding of relevant content, criticism and advice are welcome.
(1) What is JSR and what is JSR223?
JSR (Java Specification Request) is a formal request to JCP (Java Community Process) to add a standardized technical specification. Anyone can submit a JSR (Java Specification Request) to add new APIs and services to the Java platform. JSR has become an important standard in the Java world.
In the Java Servlet specification (Servlet 2.4, JSR-154), a series of core abstract concepts (various objects that need to be considered during WEB program processing) are defined to allow Java programmers to write WEB programs, including session, request, response, etc. When programmers write programs, they can communicate with these objects easily and safely. JSR223 describes how these Java objects can be opened to WEB pages written in other scripting languages, so that other languages can also access these objects. Currently this specification will be used for PHP, but this concept is independent of scripting languages, which means that it may be applied to other scripting languages in addition to PHP in the future. In a word, the purpose of JSR223 is to integrate scripting languages into the Java platform.
(2) What is PHP/Java Bridge?
PHP/Java Bridge contains a PHP module (including java.so, php_java.dll) and a related back-end program (JavaBridge.jar, JavaBridge.war or MonoBridge.exe), which is used to connect PHP's object system to Java or ECMA 335 (CLI, an important subset of the Microsoft .NET Framework) virtual machine. It fully implements the JSR 223 specification request and enables PHP scripts to access CLR (such as VB.NET, C#) or Java (Java, KAWA, JRuby)-based applications. PHP/Java Bridge communicates with the virtual machine through a local socket using an efficient communication protocol. A multi-process HTTP server will have a corresponding virtual machine process for each PHP process that handles requests.
Requests from multiple HTTP servers will be centrally sent to an application server running PHP/Java Bridge, or each HTTP server has a PHP/Java Bridge and communicates with a J2EE application server, required client The class library (ejb client.jar) will be loaded at runtime.
If there is at least one backend program running in the ECMA virtual machine, such as Novell's MONO or Microsoft's .NET, then ECMA 335-based classes can be accessed and can also support varargs, reflection, assembly loading and other features .
If there is a back-end program running in a J2EE environment, session sharing can be achieved between PHP and JSP, and clustering and load balancing can be achieved.
Unlike previous attempts (ext/java or JSR223 examples), PHP/Java Bridge no longer uses JNI (Java Native Interface). PHP objects will come from the HTTP (Apache/IIS) pool, while Java/J2EE objects will be allocated from the backend program. Objects communicate through "continuation passing style", see java_closure() below. Once a PHP object crashes, the Java application server and servlet engine will not be affected.
Starting from PHP/Java Bridge version 3.0, we can also use Java as a running environment for PHP scripts. Java code can distribute and call PHP scripts through an external or internal Java pool. Script objects can come from the external HTTP pool, the internal php FastCGI pool or use php CGI directly. This feature requires Java 6 (expected to be released in the first half of 2006) or the external javax.script package.