BSF Script Engine

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.