选择显示字体大小

连接php和 java -- php/java bridge [2]

 

(三)为什么要使用 php/java bridge?xml:namespace prefix = o />

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

即使是一些小任务,也可能需要用到java classjava类库,例如需要跨平台地生成word,excelpdf文档。

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

上面是说为什么php需要java。而对于java程序员来说,phpphp/java bridge也可能是有用的。 现在有许多基于jsp模板系统的技术如jakarta struts及更新一代技术java server facesjsp和自定义标签库有很多缺陷,把它们整合在一起去建立一个面向对象的web framework暴露了这些问题。即使jsf的作者也承认了这样的系统是有严重缺陷的,并推荐像tapestryfacelets 这样用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.

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.

 

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.



 

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


<!--[if !supportlinebreaknewline]-->
<!--[endif]-->

本模块在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种不同模式下运行:

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


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons