BSF is the abbreviation of bean Scripting framework.
Http://jakarta.apache.org/bsf/
It is an open-source project that supports calling a script in a Java application and directly accessing Java objects and methods.
BSF was initially a project of IBM's Alpha workgroup and later contributed to Apache
: Http://jakarta.apache.org/site/downloads/downloads_bsf.cgi
It can be used in Java applications to use JavaScript, Python, XSLT, Perl, TCL ,...... And other scripting languages. In turn, you can call any registered JavaBean or Java object in these scripting languages.
This is a good idea. Java can be better integrated with various popular footfall languages to realize interaction between different languages.
Sample Code (using JavaScript as the testing script language ):
Jar package required:
BSF. Jar
JS. Jar
Import org. Apache. BSF. bsfengine;
Import org. Apache. BSF. bsfexception;
Import org. Apache. BSF. bsfmanager;
/**
* BSF (bean Scripting framework) test
*
* @ Author Archie
*
* Since 09:45:02
*/
Public class bsftest {
Public static void main (string [] ARGs ){
// Script
String script =
"Function dealbean ()" +
"{"
+ "Str = BSF. lookupbean ('U ');"
+ "Return \" ID: \ "+ Str. ID + \ "username: \" + Str. uname + \ "Address: \" + Str. address + \ "_ call method: \" + Str. tostring () + "+
The bean content of '\ t' + \ "is obtained; \"; "+
"}";
Try {
// BSF Manager
Bsfmanager = new bsfmanager ();
// BSF Engine
Bsfengine = bsfmanager. loadscriptingengine ("JavaScript ");
/**
* Bean
*/
User u = new user ();
U. setid (1 );
U. setuname ("Archie ");
U. setaddress ("Shanghai Songjiang ");
// Execute the script
Bsfengine. eval ("JavaScript", 0, 0, script );
// Bsfmanager registers a bean
Bsfmanager. registerbean ("u", U );
// Bsfmanager. registerbean ("u", "Archie ");
// Execute the method in the script and return
Object result = bsfengine. eval ("JavaScript", 0, 0, "dealbean ();");
System. Out. println (result. tostring ());
} Catch (bsfexception e ){
E. printstacktrace ();
}
}
}
Running effect: