Connect PHP and Java -- PHPJavaBridge [2] Haohappy
PHP5 Research Center: http://blog.csdn.net/haohappy2004/
(3) Why PHP/Java Bridge?
Components in PHP are both short-lived and non-persistent. For complex application systems, we need to provide intermediate layer components (such as java beans/EJBs) or enterprise-level cache, connection pool or commercial logic for pages generated by PHP components. For example, parsing XML files is a resource-consuming task and requires caching. connecting to a database is also a resource-consuming task, so it must be reused. Standard php xml and DB abstraction layers are inefficient because they cannot implement cache and connection pools through an intermediate layer.
Even for some small tasks, Java Class or Java Class Library may be used. for example, you need to generate Word, Excel, or PDF documents across platforms.
PHP, PHP/Java Bridge, and php code can be packaged into a standard J2EE archive format. you can easily deploy the code in a J2EE application server or servlet engine. Users do not need to install PHP. from their perspective, they cannot see the differences between JSP, servlet, and PHP pages. Since Bridge allows session sharing between PHP and J2EE, developers can integrate JSP-based programs with PHP step by step.
The above explains why PHP needs Java. For Java programmers, PHP and PHP/Java Bridge may also be useful. Currently, many technologies based on JSP template systems, such as jakarta Struts and Java Server Faces, are available. JSP and custom tag libraries have many defects. integrating them together to build an object-oriented WEB Framework exposes these problems. Even the author of JSF admitted that such a system has serious defects, we recommend that you use Java classes like tapestry or facelets to define components and bind them to XML/HTML templates by their IDs. PHP/Java Bridge version 3.0 can be embedded with PHP code into the JSF Framework, so that the user interface designer is focused on designing HTML templates, while programmers can build prototypes using PHP, and use the existing development framework. Many large websites now use PHP on the front end, while the core uses Java to build the system.
PHP/Java Bridge adds the following primitive types and functions to PHP, so that PHP can easily access Java objects. Table 1 shows the distribution of data types.
New Java ("CLASSNAME"): References and instancates the class CLASSNAME. After script execution the referenced classes may be garbage collected.
New JavaClass ("CLASSNAME"): References the class CLASSNAME without creating an instance. the object returned is the class object itself, not an object of the class. after script execution the referenced classes may be garbage collected. example:
Java_require ("JAR1; JAR2"): Makes additional libraries available to the current script. JAR can either be a "http:", "ftp:", "file:" or a "jar:" location. on "Security Enhanced Linux" (please see the README) the location must be tagged with a lib_t security context.
Java_context (): Makes the javax. script. ScriptContext available to the current script. Available since PHP/Java Bridge version 3.0.
Java_closure (ENVIRONMENT, MAP, TYPE): Makes it possible to call PHP code from java. it closes over the PHP environment and packages it up as a java class. if the ENVIRONMENT is missing, the current environment is used. if MAP is missing, the PHP procedures must have the same name as the required procedures. if TYPE is missing, the generated class is "generic", I. e. the interface it implements is determined when the closure is applied.
The java_closure can also be used to emulate try/catch functionality in PHP4.
$ Session = java_session (): Creates or retrieves a session context. When the backend is running in a J2EE environment, the session is taken from the request object, otherwise it is taken from PHP.
$ Session = java_session (SESSIONNAME): Creates or retrieves the session SESSIONNAME. This primitive uses a "private" session store with the name SESSIONNAME.
The java_session primitive is meant for values which must keep VE the current script. If you want to cache data which is expensive to create, bind the data to a class.
JavaException: A java exception class. Available in PHP 5 and above only. Example:
Foreach (COLLECTION): It is possible to iterate over values of java classes that implement java. util. Collection or java. util. Map. Available in PHP 5 and above only.
[Index]: It is possible to access elements of java arrays or elements of java classes that implement the java. util. Map interface. Available in PHP 5 and above only.
Java_instanceof (JAVA_OBJ, JAVA_CLASS): Tests if JAVA_OBJ is an instance of JAVA_CLASS.
Java_last_exception_get (): Returns the last exception instance or null. since PHP 5 you can use try/catch instead. java_last_exception_clear (): Clears the error condition. since PHP 5 you can use try/catch instead.
Table 1. data type ing Table
| PHP |
Java |
Description |
Example |
| Object |
Java. lang. Object |
An opaque object handle. however, we guarantee that the first handle always starts with 1 and that the next handle is n + 1 for all n <1024 (useful if you work with the raw XML protocol, see the python and scheme examples ). |
$ Buf = new java ("java. io. ByteArrayOutputStream "); $ Outbuf = new java ("java. io. PrintStream", $ buf ); |
| Null |
Null |
NULL value |
$ Outbuf-> println (null ); |
| Exact number |
Long |
64-bit integer |
$ Outbuf-& gt; println (100 ); |
| Boolean |
Boolean |
Boolean value |
$ Outbuf-> println (true ); |
| Inexact number |
Double |
IEEE floating point |
$ Outbuf-& gt; println (3.14 ); |
| String |
Byte [] |
Binary data, unconverted |
$ Bytes = $ buf-> toByteArray (); |
| String |
Java. lang. String |
An UTF-8 encoded string. since PHP does not support unicode, all java. lang. string values are auto-converted into a byte [] (see above) using UTF-8 encoding. the encoding can be changed with the java_set_file_encoding () primitive. |
$ String = $ buf-> toString (); |
| Array (as array) |
Java. util. Collection or T [] |
PHP4 sends and distance es arrays as values. PHP5 sends arrays as values and between es object handles which implement the new iterator and array interface. |
// Pass a Collection to Vector $ Ar = array (1, 2, 3 ); $ V = new java ("java. util. Vector", $ ar ); Echo $ v-> capacity (); // Pass T [] to asList () $ A = new JavaClass ("java. util. Arrays "); $ Lst = $ A-> asList ($ ar ); Echo $ lst-> size (); |
| Array (as hash) |
Java. util. Map |
PHP4 sends and has es hashtables as values. PHP5 sends hashtables as values and has es object handles which implement the new iterator interface. |
$ H = array ("k" => "v", "k2" => "v2 "); $ M = new java ("java. util. HashMap", $ h ); Echo $ m-> size (); |
| JavaException |
Java. lang. Exception |
A wrapped exception class. The original exception can be retrieved with $ exception-> getCause (); |
... Catch (JavaException $ ex ){ Echo $ ex-> getCause (); } |
This module is available in Mandrake Linux 9.2, RedHat Enterprise 3, RedHat Fedora Core 1 .. 4. FreeBSD 5.3, Solaris 9 (iSCSI, 64-bit JVM) and Windows RedHat (Cygwin virtual) platforms have passed tests. Theoretically, they can run on all * nix operating systems.
PHP/Java Bridge can run in four different modes:
It is called through the dl () function. In this mode, when the HTTP server receives a client request and returns the result, it starts to call PHP/Java Bridge. The speed is slow because you have to restart the Java virtual machine for each request, but you do not need any system management permissions. The global configuration file php. ini is permanently activated through the Java process running inside the HTTP server. In this mode, PHP/Java Bridge is started after the HTTP server is started and stopped. This mode is recommended during development and testing. The global configuration file php. ini is permanently activated through external Java processes. It is recommended to be used in the product system (that is, when the system is running ). This mode is similar to the RPM package implementation method in RedHat Linux. In this mode, PHP/Java Bridge serves as the system service. Activate the php. ini file through an external application server or servlet engine. PHP can be used as a CGI sub-component of a pure Java application server or installed as an Apache/IIS module.
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? PostId = 627890