当前位置:首页 > CN2资讯 > 正文内容

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

2天前CN2资讯


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


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

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

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

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

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


    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/26579.html

    分享给朋友:

    “连接php和 java -- php/java bridge [2]” 的相关文章

    绿云:数字化转型与创新解决方案的行业领导者

    绿云在多个领域的业务发展展现了其强大的行业影响力。从数字乡村服务到酒店数字化解决方案,绿云的创新模式和技术实力为其赢得了广泛的市场认可。 绿云信息有限公司的数字乡村服务 通辽市绿云信息有限公司作为数字乡村服务的领军企业,专注于三农领域的信息化服务。公司通过提供数字农业、乡村治理、农业农村大数据和创新...

    解决BestTrace中的timestamp is error问题及优化网络性能指南

    BestTrace是一款强大的网络诊断工具,广泛用于追踪数据包从源头到目标的网络路径。它的工作原理结合了traceroute和ping的功能,让用户不仅能够查看每一跳的延迟,还能监测到丢包情况。这意味着,你在使用BestTrace时,能够获得关于网络连接质量的详细信息,及时发现潜在的问题。 在我实际...

    搬瓦工VPS与IPv6: 优化你的网络体验

    搬瓦工(BandwagonHost)作为一家由加拿大IT7 Networks公司推出的品牌,专注于提供性价比较高的VPS主机服务。我一直对VPS的体验充满好奇,尤其是搬瓦工的背景与发展历程。最初,搬瓦工主要销售超低价的OpenVZ方案,吸引了不少预算有限的用户。随着技术的发展和市场需求的变化,搬瓦工...

    阿里云国际站:轻松注册与支付方式全解析

    什么是阿里云国际站? 在这个科技迅速发展的时代,云计算已经成为许多企业和个人开展业务的重要基础设施。阿里云国际站便是一处全球化的云计算服务平台,旨在为世界各地的用户提供高效、灵活和安全的云计算服务。它不仅支持多种功能,还具备强大的全球基础设施,能够满足不同用户的需求。 阿里云国际站被设定为一个面向全...

    Hostwinds LLC:卓越的网络托管服务与高性价比优势

    Hostwinds LLC成立于2010年,位于美国西雅图。这家公司一直专注于提供多种网络托管服务,包括虚拟主机、虚拟专用服务器(VPS)和独立服务器。在这个竞争激烈的市场中,Hostwinds凭借其独特的优势和不断升级的服务赢得了客户的信赖。我个人认为,Hostwinds的历史反映了它对客户需求的...

    BBR对国内网站的实际作用与应用效果分析

    BBR(Bottleneck Bandwidth and Round-trip propagation time)算法是由Google推出的一种TCP拥塞控制算法。它的设计初衷是为了优化网络连接的传输速率和稳定性,尤其是在面临高延迟和波动网络条件时表现优异。可能的很多朋友会问,BBR到底是个什么东西...