Rumah > pembangunan bahagian belakang > tutorial php > 连接PHP和 Java -- PHP/Java Bridge [2]

连接PHP和 Java -- PHP/Java Bridge [2]

WBOY
Lepaskan: 2016-06-23 14:35:39
asal
923 orang telah melayarinya

Haohappy
PHP5研究中心:http://blog.csdn.net/haohappy2004/


(三)为什么要使用 PHP/Java Bridge?

PHP中的组件都是短暂,非持久化的。如果是复杂应用体系,我们需要提供中间层组件(像java beans/EJB)或者企业级的提供缓存,连接池或商业逻辑给PHP组件生成的页面。例如解析XML文件就是一个比较耗资源的工作,需要缓存;连接数据库也是个比较耗资源的工作,所以需要能够重用。标准的PHP XML和DB抽象层都是比较没效率的,因为它们都不能通过一个中间层来实现缓存和连接池。

即使是一些小任务,也可能需要用到Java Class或Java类库,例如需要跨平台地生成Word,Excel或PDF文档。

PHP,PHP/Java Bridge和php代码可以打包成标准的J2EE档案包格式,用户可以方便地布置到一个J2EE应用服务器或servlet引擎中去。用户不需要安装PHP,而且从他们的角度来说,他们看不到这些用JSP,servlet和PHP生成的页面有什么区别。由于Bridge允许PHP和J2EE间的session共享,开发者可以一步步地把基于JSP的程序和PHP集成起来。

上面是说为什么PHP需要Java。而对于Java程序员来说,PHP和PHP/Java Bridge也可能是有用的。 现在有许多基于JSP模板系统的技术如jakarta Struts及更新一代技术Java Server Faces。JSP和自定义标签库有很多缺陷,把它们整合在一起去建立一个面向对象的WEB Framework暴露了这些问题。即使JSF的作者也承认了这样的系统是有严重缺陷的,并推荐像tapestry或facelets 这样用Java类定义组件并通过他们的ID来绑定到XML/HTML模板中。PHP/Java Bridge version 3.0可以嵌入PHP代码到JSF Framework中,这样用户界面设计师集中精力在设计HTML模板,而程序员可以用PHP建立原型,并使用已有的开发框架。现在不少大型站点就在前端使用PHP,而核心使用Java来构建系统。

PHP/Java Bridge添加了下面这些原始类型和函数到PHP中,以使PHP可以方便地访问Java对象。在表格1中可以看到数据类型的分布情况。

new Java("CLASSNAME"): References and instanciates 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 survive 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. 数据类型映射表

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

$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->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 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 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();
}



本模块在Mandrake Linux 9.2,RedHat Enterprise 3, RedHat Fedora Core 1..4, FreeBSD 5.3, Solaris 9 (Sparc, 64 bit JVM) 和 Windows 下RedHat (Cygwin虚拟)各种平台均通过测试, 理论上说可以运行于所有*nix操作系统。

PHP/Java Bridge可以在4种不同模式下运行:

通过dl()函数调用。在这种模式下,当HTTP服务器接收一个客户请求并反映结束后,开始调用PHP/Java Bridge。速度很慢,因为为每次请求都要重新启动Java虚拟机,但不需要任何系统管理权限。 通过HTTP服务器内部运行的Java进程持久性地激活全局配置文件php.ini。这种模式下PHP/Java Bridge是在HTTP服务器启动后启动,停止后停止。在开发测试过程中推荐使用这种模式。 通过外部的Java进程来持久性地激活全局配置文件php.ini。推荐用于产品系统中(即系统正式运行时)。这种模式类似于RedHat Linux下RPM包的实现方式。这种模式下PHP/Java Bridge作为系统服务。 通过外部的应用服务器或servlet引擎来激活php.ini文件。PHP可以作为纯Java应用服务器的CGI子组件运行,或者安装为Apache/IIS模块。

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=627890


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan