Connecting PHP and Java-Php/java Bridge [2]

Source: Internet
Author: User
Haohappy
PHP5 Research Center: HTTP://BLOG.CSDN.NET/HAOHAPPY2004/


(c) Why use Php/java Bridge?

The components in PHP are ephemeral and non-persistent. In the case of complex applications, we need to provide middle-tier components (like Java BEANS/EJB) or enterprise-level caching, connection pooling or business logic to the pages generated by the PHP component. Parsing an XML file, for example, is a resource-intensive effort that requires caching, and connecting to a database is a resource-intensive effort, so it needs to be reusable. The standard PHP XML and DB abstraction layers are inefficient because none of them can implement caching and connection pooling through a middle tier.

Even small tasks may require Java classes or Java class libraries, such as the need to build Word,excel or PDF documents across platforms.

Php,php/java Bridge and PHP code can be packaged into a standard Java EE package format that users can easily decorate into a Java EE application Server or servlet engine. Users do not need to install PHP, and from their point of view they do not see the difference between these pages generated with Jsp,servlet and PHP. Because bridge allows session sharing between PHP and the Java EE, developers can step through the integration of JSP-based programs with PHP.

It says why PHP needs java. For Java programmers, PHP and Php/java Bridge can also be useful. There are now many technologies based on JSP templating systems, such as Jakarta struts and the next generation of Java Server Faces. JSP and custom tag libraries have a lot of flaws, and incorporating them together to create an object-oriented web framework exposes these issues. Even if the JSF author admits that such a system is severely flawed, it is recommended to define components in Java classes like tapestry or facelets and bind them to xml/html templates with their IDs. Php/java Bridge version 3.0 can embed PHP code into the JSF framework so that the user interface designer is focused on designing HTML templates, and programmers can build prototypes with PHP and use existing development frameworks. Many large sites now use PHP at the front end, and 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. You can see the distribution of data types in table 1.

New Java ("CLASSNAME"): References and Instanciates the class CLASSNAME. After script execution the referenced classes is garbage collected.

New Javaclass ("CLASSNAME"): References the class CLASSNAME without creating an instance. The object returned is the class object itself and not a object of the class. After script execution the referenced classes is garbage collected. Example:

Java_require ("JAR1; JAR2 "): Makes additional libraries available to the current script. Jar can either is a "http:", "ftp:", "File:" or a "jar:" Location. On "Security Enhanced Linux" (please see the README) the location must is 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 has the same name as the required procedures. If TYPE is missing, the generated class is "generic", i.e. the interface it implements was determined when the closure was a Pplied.

The java_closure can also is used to emulate Try/catch functionality in PHP4.

$session =java_session (): Creates or retrieves a session context. When the backend was running in a EE environment, the session was taken from the request object, otherwise it was taken fro M 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 survive the current script. If you want to cache data which are 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's possible to iterate over values of Java classes that implement Java.util.Collection or Java.uti L.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 int Erface. 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 can use Try/catch instead. Java_last_exception_clear (): Clears the error condition. Since PHP 5 can use Try/catch instead.

Table 1. Data Type mapping table

Php

Java

Description

Example

Object

Java.lang.Object

An opaque object handle. However, we guarantee the first handle always starts with 1 and that the next handle are n+1 for all n < 1024x768 (Usef UL 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

A bit integer

$outbuf->println (100);

Boolean

Boolean

Boolean value

$outbuf->println (TRUE);

inexact number

Double

IEEE floating Point

$outbuf->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 is auto-converted into a byte[] (see above) using UTF-8 E Ncoding. The encoding can changed with the java_set_file_encoding () primitive.

$string = $buf->tostring ();

Array (as Array)

Java.util.Collection or t[]

PHP4 sends and receives arrays as values. PHP5 sends arrays as values and receives 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 receives hashtables as values. PHP5 sends hashtables as values and receives 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 in the Mandrake Linux 9.2,redhat Enterprise 3, RedHat Fedora Core 1..4, FreeBSD 5.3, Solaris 9 (Sparc, a-bit JVM) and Windows Under Redhat (Cygwin virtual) various platforms are tested and theoretically can run on all *nix operating systems.

Php/java Bridge can operate in 4 different modes:

Called by the DL () function. In this mode, when the HTTP server receives a client request and reflects the end, it starts calling Php/java Bridge. Is slow because the Java virtual machine is restarted for each request, but no system administrative permissions are required. The global configuration file, PHP.ini, is persisted by the Java process running inside the HTTP server. In this mode Php/java bridge is started after the HTTP server starts and stops after stopping. This pattern is recommended during development testing. The global profile php.ini is permanently activated through an external Java process. Recommended for use in product systems (i.e., when the system is fully operational). This pattern is similar to the implementation of RPM packages under Redhat Linux. This mode Php/java bridge as a system service. Activate the php.ini file via an external application server or Servlet engine. PHP can be run as a CGI sub-component of a pure Java application Server, or as a apache/iis module.

trackback:http://tb.blog.csdn.net/trackback.aspx?postid=627890

  • 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.