BSF及其使用
来源:互联网 发布:淘宝女士内裤买家秀 编辑:程序博客网 时间:2024/06/10 00:30
BSF Architectural OverviewBean Scripting Framework (BSF) is a set of Java classes which provides scripting language support within Java applications, and access to Java objects and methods from scripting languages.
InstallationThe two primary components of BSF are the
BSFManager
and theBSFEngine
.The
BSFManager
handles all scripting execution engines running under its control, and maintains the object registry that permits scripts access to Java objects. By creating an instance of theBSFManager
class, a Java application can gain access to scripting services.The
BSFEngine
provides an interface that must be implemented for a language to be used by BSF. This interface provides an abstraction of the scripting language's capabilities that permits generic handling of script execution and object registration within the execution context of the scripting language engine.An application can instantiate a single
BSFManager
, and execute several different scripting languages identically via theBSFEngine
interface. Furthermore, all of the scripting languages handled by theBSFManager
are aware of the objects registered with thatBSFManager
, and the execution state of those scripting languages is maintained for the lifetime of theBSFManager
.
Using BSFBSF can be used standalone, as a class library, or as part of an application server. In order to be used as a class library or as a standalone system, you simply download a copy of the bsf.jar file from the BSF web site and include it in your classpath, along with any required classes or jar files for desired languages.
In order to use BSF as part of the Tomcat servlet engine, you must currently download patches from the BSF web site that permit Jasper to call BSF. Instructions for this will be posted on the website, and will soon be accompanied by prebuilt binaries. We hope that these changes will be merged into Tomcat in the near future.
BSF and JSPsAdding BSF Support for a Scripting LanguageServlets and Other ApplicationsAfter you set up an application server that is BSF enabled, you can write JSPs using any of the supported scripting languages. JSPs using scripting languages differ only slightly from those using Java.
First, you must set the language attribute of the page directive in the JSP to the desired language. For example,
sets the language used for the JSP to Javascript; any
<%@ page language="javascript" %>
scriptlet
s orexpressions
within the JSP will be handed off to BSF, which will in turn hand the code over to Rhino for execution.The standard set of JSP implicit objects is available within BSF. These implicit objects must be used for input and output with respect to the generated page, since the scripting languages do not have any awareness of having been called within a JSP. For example, in order to print a line of text into the page generated by the JSP, one must use the
println()
method of theout
implicit object.Multiple languages can be supported within a given JSP; this is accomplished by using the BSF taglibs, which are available from the Jakarta Taglibs project. BSF taglib provides two tags:
scriptlet
andexpression
. Both of these have a required language attribute, which is used to specify the language used on a perscriptlet
orexpression
basis.Using BSF in servlets or applications is also quite simple. In order to provide an application with scripting support, you need to import the BSF class hierarchy and instantiate a
BSFManager
object. After instantiating theBSFManager
, you register or declare any Java objects to be made available within the scripting engine. Then call either one of theeval()
orexec() BSFManager
methods (depending on whether you want to evaluate a script and have the value of the evaluation returned, or execute a script). Alternatively, you can call theloadScriptingEngine()
method in order to get an object implementing theBSFEngine
interface for the desired scripting language. You can then call theexec()
oreval()
methods ofBSFEngine
to run the script.Additionally, BSF declares an object named
bsf
within a scripting engine's execution context, which represents theBSFManager
that is associated with the scripting engine. This object provides all of the methods and properties associated with theBSFManager
to the script. However, the most used method within scripts is usuallylookupBean()
, which is used to access objects in BSF's object registry.The most important methods within the
BSFManager
are:
BSFManager
() - theBSFManager
constructoreval()
- used to evaluate a script and return its valueexec()
- used to execute a scriptloadScriptingEngine()
- used to return aBSFEngine
for the desired scripting languageregisterBean()
- adds an object to BSF's object registrylookupBean()
- retrieves an object from BSF's object registrydeclareBean()
- creates an implicit object in the context of any loaded scripting language, which does not have to be accessed vialookupBean()
Other, less often used methods within the
BSFManager
are:
apply()
- used to call anonymous functionscompileExpr()
- used to compile an expression into aCodeBuffer
objectcompileScript()
- similar to compile expression, used to compile scripts intoCodeBuffer
objectscompileApply()
- similar to both of the above - used to compile anonymous functions intoCodeBuffer
objectsFor the curious, the
CodeBuffer
is a class provided by BSF for storing generated Java code.The
BSFManager
exec()
,eval()
, andapply()
methods (as well as their compile counterparts) are wrappers over the equivalent methods presented by theBSFEngine
interface. If the programmer explicitly loads a scripting engine vialoadScriptingEngine()
, they can use theexec()
oreval()
methods of the resultingBSFEngine
as appropriate.
Standalone ScriptsIn order to incorporate your own scripting language into BSF, you must first write a class implementing the
BSFEngine
interface for the language; examples are available in the BSF source distribution.Usually, a scripting language author extends the
BSFEngineImpl
class, which implementsBSFEngine
, and only requires the scripting language author to implement theeval()
method. However, the following methods specified by theBSFEngine
interface are the most commonly implemented:
initialize()
- used to set up the underlying scripting language enginecall()
- used to call functions or methods within the scripting engineeval()
- used to evaluate a scriptexec()
- used to execute a scriptdeclareBean()
- used to create an implicit object within the scripting languageundeclareBean()
- used to remove an implicit object from the scripting languageOnce you have implemented the wrapper for your language engine, you instantiate a
BSFManager
in your application, and register your engine with it via theregisterScriptingEngine()
method. Afterward, you may use your language within the application through the usual BSF semantics.
BSF provides a facility for running scripting languages itself. Simply running
java org.apache.bsf.Main
will produce a help message, with instructions on how to run these scripts.
- BSF及其使用
- BSF
- bsf.jar
- BSF BDF
- intel bsf指令
- BSF | 1915 | Knight Moves
- BSF二叉查找树
- 逆序的BSF输出
- bsf 迷宫三维
- bsf find a way
- 优先bsf rescue
- [Algorithm]九章四:BSF
- uva 10821 - Constructing BST(BSF)
- rhino Javascript Engine, BSF及多线程应用
- 查询BSF资料总结【未写完】
- PKU ACM 1753 第一道bsf算法
- 迷宫问题bsf ACM第三次培训
- poj 3414 Pots(BSF + 记录路径)
- 神州数码私有报文分析完毕,庆祝一下[神州数码认证客户端破解]
- 深入浅出Linux操作系统的优化和微调
- 浅谈Linux操作系统的优化及安全问题
- 各类开源协议介绍
- 什么样的界面算是好界面
- BSF及其使用
- 喜欢上一款诺基亚的手机,nokia5500
- SIP协议全方位概要介绍
- 关于chroot介绍
- Linux日志分析
- 没多少时间写blog
- HP2210硬件重启的方法
- Linux系统常见紧急情况的处理方法
- 关于getline函数的疑问