`
thomas0988
  • 浏览: 473884 次
  • 性别: Icon_minigender_1
  • 来自: 南阳
社区版块
存档分类
最新评论

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

阅读更多

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

(三)为什么要使用 PHP/Java Bridge?<o:p></o:p>

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

即使是一些小任务,也可能需要用到Java ClassJava类库,例如需要跨平台地生成Word,ExcelPDF文档。<o:p></o:p>

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

上面是说为什么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来构建系统。<o:p></o:p>

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. <o:p></o:p>

  • 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: <o:p></o:p>

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. <o:p></o:p>

java_context(): Makes the javax.script.ScriptContext available to the current script. Available since PHP/Java Bridge version 3.0.<o:p></o:p>

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. <o:p></o:p>

The java_closure can also be used to emulate try/catch functionality in PHP4. <o:p></o:p>

$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. <o:p></o:p>

$session=java_session(SESSIONNAME): Creates or retrieves the session SESSIONNAME. This primitive uses a "private" session store with the name SESSIONNAME. <o:p></o:p>

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. <o:p></o:p>

  • JavaException: A java exception class. Available in PHP 5 and above only. Example:<o:p></o:p>

<o:p> </o:p>

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. <o:p></o:p>

[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. <o:p></o:p>

java_instanceof(JAVA_OBJ, JAVA_CLASS): Tests if JAVA_OBJ is an instance of JAVA_CLASS. <o:p></o:p>

  • java_last_exception_get(): Returns the last exception instance or null. Since PHP 5 you can use try/catch instead.<o:p></o:p>
  • java_last_exception_clear(): Clears the error condition. Since PHP 5 you can use try/catch instead. <o:p></o:p>


<o:p> </o:p>

Table 1. 数据类型映射表<o:p></o:p>

PHP<o:p></o:p>

Java<o:p></o:p>

Description<o:p></o:p>

Example<o:p></o:p>

object<o:p></o:p>

java.lang.Object<o:p></o:p>

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).<o:p></o:p>

$buf=new java("java.io.ByteArrayOutputStream");
$outbuf=new java("java.io.PrintStream", $buf);<o:p></o:p>

null<o:p></o:p>

null<o:p></o:p>

NULL value<o:p></o:p>

$outbuf->println(null);<o:p></o:p>

exact number<o:p></o:p>

long<o:p></o:p>

64 bit integer<o:p></o:p>

$outbuf->println(100);<o:p></o:p>

boolean<o:p></o:p>

boolean<o:p></o:p>

boolean value<o:p></o:p>

$outbuf->println(true); <o:p></o:p>

inexact number<o:p></o:p>

double<o:p></o:p>

IEEE floating point<o:p></o:p>

$outbuf->println(3.14); <o:p></o:p>

string<o:p></o:p>

byte[]<o:p></o:p>

binary data, unconverted<o:p></o:p>

$bytes=$buf->toByteArray();<o:p></o:p>

string<o:p></o:p>

java.lang.String<o:p></o:p>

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.<o:p></o:p>

$string=$buf->toString();<o:p></o:p>

array (as array)<o:p></o:p>

java.util.Collection or T[]<o:p></o:p>

PHP4 sends and receives arrays as values. PHP5 sends arrays as values and receives object handles which implement the new iterator and array interface.<o:p></o:p>

// 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(); <o:p></o:p>

array (as hash)<o:p></o:p>

java.util.Map<o:p></o:p>

PHP4 sends and receives hashtables as values. PHP5 sends hashtables as values and receives object handles which implement the new iterator interface.<o:p></o:p>

$h=array("k"=>"v", "k2"=>"v2");
$m=new java("java.util.HashMap",$h);
echo $m->size(); <o:p></o:p>

JavaException<o:p></o:p>

java.lang.Exception<o:p></o:p>

A wrapped exception class. The original exception can be retrieved with $exception->getCause();<o:p></o:p>

...
catch(JavaException $ex) {
echo $ex->getCause();
} <o:p></o:p>


本文地址:http://www.newbooks.com.cn/info/8934.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics