Most developers have heard of, and possibly used, scripting languages such as Ruby, JavaScript, and Python. These dynamic languages are enjoying a resurgence in popularity, largely because of their flexibility and simplicity, and the productivity gains they promise.
Java 6 comes with built-in support for scripting languages. You can embed scripts in various scripting languages into your Java applications, passing parameters, evaluating expressions, and retrieving results. And you can do it all pretty seamlessly.
First of all, you obtain a new ScriptEngine object from a ScriptEngineManager, as shown here:
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Each scripting language has its own unique identifier. The "js" here means you're dealing with JavaScript.
Now you can start having some fun. Interacting with a script is easy and intuitive. You can assign scripting variables using the put() method and evaluate the script using the eval() method,. which returns the most recently evaluated expression processed by the script. And that pretty much covers the essentials. Here's an example that puts it all together:
engine.put("cost", 1000);
String decision = (String) engine.eval(
"if ( cost >= 100){ " +
"decision = 'Ask the boss'; " +
"} else {" +
"decision = 'Buy it'; " +
"}");
assert ("Ask the boss".equals(decision));
You can do more than just pass variables to your scripts— you can also invoke Java classes from within your scripts. Using the importPackage() function enables you to import Java packages, as shown here:
engine.eval("importPackage(java.util); " +
"today = new Date(); " +
"print('Today is ' + today);");
Another cool feature is the Invocable interface, which lets you invoke a function by name within a script. This lets you write libraries in scripting languages, which you can use by calling key functions from your Java application. You just pass the name of the function you want to call, an array of Objects for the parameters, and you're done! Here's an example:
engine.eval("function calculateInsurancePremium(age) {...}");
Invocable invocable = (Invocable) engine;
Object result = invocable.invokeFunction("calculateInsurancePremium",
new Object[] {37});
You actually can do a fair bit more than what I've shown here. For example, you can pass a Reader object to the eval() method, which makes it easy to store scripts in external files, or bind several Java objects to JavaScript variables using a Map-like Binding object. You can also compile some scripting languages to speed up processing. But you probably get the idea that the integration with Java is smooth and well thought-out.
剛剛結束了IBM的電面,一個GG和JJ,問的主要問題都放在我的項目上了,尤其是Netbeans的插件。由于我申請的實習生,所以在之中也問了很多實習時間的問題,感覺IBM問問題很不含糊,甚至要問出幾月幾號......這一點可以說明IBM是一個很值得人去的地方。英文問題我感覺我答得不是很好,先問得IBM的文化之類的,之后居然問得IBM person慚愧啊 本來還知道幾個的,一緊張都忘了。在角色兌換時,我問得我的表現,和職業生涯建議,好小子,他們居然還反問我職業生涯,說了一通......整個電面是從4:10-4:45,最后GG說面試表現還好,只是我能實習的時間比他們要求的時間要少,唉~~ 我的系主任啊,等待結果中....希望自己能去IBM體驗一下藍色巨人的風范吧。一會4:30要參加北京IBM研究院的一個電面,快到時間了,不免心里稍有些緊張,不知道先前積累的一點英語一會是否會奏效,呵呵,,,,不過,無論如何我會盡可能抓住這次來之不易的機會的。 摘要: This post had been writen long before,...but 閱讀全文 摘要: Java SE 6.0(代號Mustang,野馬)已經發布,詳情請見 野馬奔騰而出,Java SE 6 正式版發布 ,它給我們帶來了哪些新的特性了。 首先,我們看看JDK 6.0包含了大量的JSR,分為四組,分別為: 在簡化開發方面: 199: Compiler API... 閱讀全文
前幾天好不容易下到了JDK6mustang,今天恰好有時間升級了一下Netbeans默認的JDK版本。這里簡單的說明一下升級的方法。如果我 們不修改Netbeans的屬性,需要在JavaPlatform manager中加入另一版本的類庫。新建工程后如果要修改類庫,還需要修改項目的類庫屬性,現在通過修改默認的JDK類庫,便可方便很多,更不需要重新 安裝NB。
我的NB裝在D盤中,可以在該路徑找到文件D:\Netbeans-5.5\etc\Netbeans.conf,我們將原有的默認類庫netbeans_jdkhome="D:\Java\jdk1.5.0_07"修改為 netbeans_jdkhome="D:\Java\jdk1.6.0"便輕松的完成了升級,當然在tools-〉JavaPlatform manager〉中當然也可以將我們慣用的D:\Java\jdk1.5.0_07加入為可選用類庫。
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>拖拽Demo</title>
<style type="text/CSS">
body
{
margin:0px;
}
#aim
{
position:absolute;
width:200px;
height:30px;
border:1px solid #666666;
background-color:#FFCCCC;
}
#sourceLayer, #cloneLayer
{
position:absolute;
width:300px;
height:50px;
border:1px solid #666666;
background-color:#CCCCCC;
cursor:move;
}
.docked
{
display:none;
filter:alpha(opacity=100);
}
.actived
{
display:block;
filter:alpha(opacity=70);
}
</style>
</head>
<body >
<div id="aim">locate</div>
<div id="sourceLayer" unselectable="off"><img src="http://www.baidu.com/img/logo.gif" alt="Drag Demo">Source of the demo</div>
<div id="cloneLayer" class="docked" unselectable="off"></div>
<script type="text/javascript" language="javascript">
var aim;
var sourceLayer;
var cloneLayer;
var aimX;
var aimY;
var orgnX;
var orgnY;
var draging = false;
var offsetX = 0;
var offsetY = 0;
var back;
var thisX ;
var thisY ;
var time ;
var stepX ;
var stepY ;
function getLayer(inAim,inSource,inClone)
{
aim = document.getElementById(inAim);
sourceLayer = document.getElementById(inSource);
cloneLayer = document.getElementById(inClone);
}
function initDrag(initAimX,initAimY,initOrgnX,initOrgnY)
{
aimX = initAimX;
aimY = initAimY;
orgnX = initOrgnX;
orgnY = initOrgnY;
aim.style.pixelLeft = aimX;
aim.style.pixelTop = aimY;
sourceLayer.style.pixelLeft = orgnX;
sourceLayer.style.pixelTop = orgnY;
cloneLayer.style.pixelLeft = orgnX;
cloneLayer.style.pixelTop = orgnY;
}
function BeforeDrag()
{
if (event.button != 1)
{
return;
}
cloneLayer.innerHTML = sourceLayer.innerHTML; 容
offsetX = document.body.scrollLeft + event.clientX - sourceLayer.style.pixelLeft;
offsetY = document.body.scrollTop + event.clientY - sourceLayer.style.pixelTop;
cloneLayer.className = "actived";
draging = true;
}
function OnDrag()
{
if(!draging)
{
return;
}
event.returnValue = false;
cloneLayer.style.pixelLeft = document.body.scrollLeft + event.clientX - offsetX;
cloneLayer.style.pixelTop = document.body.scrollTop + event.clientY - offsetY;
}
function EndDrag()
{
if (event.button != 1)
{
return;
}
draging = false;
if (event.clientX >= aim.style.pixelLeft && event.clientX <= (aim.style.pixelLeft + aim.offsetWidth) &&
event.clientY >= aim.style.pixelTop && event.clientY <= (aim.style.pixelTop + aim.offsetHeight))
{
sourceLayer.style.pixelLeft = aim.style.pixelLeft;
sourceLayer.style.pixelTop = aim.style.pixelTop;
cloneLayer.className = "docked";
}
else
{
thisX = cloneLayer.style.pixelLeft;
thisY = cloneLayer.style.pixelTop;
offSetX = Math.abs(thisX - orgnX);
offSetY = Math.abs(thisY - orgnY);
time = 500;
stepX = Math.floor((offSetX/time)*20);
stepY = Math.floor((offSetY/time)*20);
if(stepX == 0)
stepX = 2;
if(stepY == 0)
stepY = 2;
moveStart();
}
}
function moveStart()
{
back = setInterval("MoveLayer();",15);
}
function MoveLayer()
{
if(cloneLayer.style.pixelLeft <= orgnX && cloneLayer.style.pixelTop <= orgnY)
{
cloneLayer.style.pixelLeft += stepX;
cloneLayer.style.pixelTop += stepY;
if(cloneLayer.style.pixelLeft > orgnX)
{
stepX = 1;
}
if(cloneLayer.style.pixelTop > orgnY)
{
stepY = 1;
}
//if the coordinate of X Y are same
if(cloneLayer.style.pixelLeft == orgnX)
{
stepX = 0;
}
if(cloneLayer.style.pixelTop == orgnY)
{
stepY = 0;
}
if(cloneLayer.style.pixelLeft == orgnX && cloneLayer.style.pixelTop == orgnY)
{
EndMove();
}
}
//locate to the downleft of the object
else if(cloneLayer.style.pixelLeft <= orgnX && cloneLayer.style.pixelTop >= orgnY)
{
cloneLayer.style.pixelLeft += stepX;
cloneLayer.style.pixelTop -= stepY;
if(cloneLayer.style.pixelLeft > orgnX)
{
stepX = 1;
}
if(cloneLayer.style.pixelTop < orgnY)
{
stepY = 1;
}
if(cloneLayer.style.pixelLeft == orgnX)
{
stepX = 0;
}
if(cloneLayer.style.pixelTop == orgnY)
{
stepY = 0;
}
if(cloneLayer.style.pixelLeft == orgnX && cloneLayer.style.pixelTop == orgnY)
{
EndMove();
}
}
else if(cloneLayer.style.pixelLeft >= orgnX && cloneLayer.style.pixelTop <= orgnY)
{
cloneLayer.style.pixelLeft -= stepX;
cloneLayer.style.pixelTop += stepY;
if(cloneLayer.style.pixelLeft < orgnX)
{
stepX = 1;
}
if(cloneLayer.style.pixelTop > orgnY)
{
stepY = 1;
}
if(cloneLayer.style.pixelLeft == orgnX)
{
stepX = 0;
}
if(cloneLayer.style.pixelTop == orgnY)
{
stepY = 0;
}
if(cloneLayer.style.pixelLeft == orgnX && cloneLayer.style.pixelTop == orgnY)
{
EndMove();
}
}
//locate to the right of the object
else if(cloneLayer.style.pixelLeft >= orgnX && cloneLayer.style.pixelTop >= orgnY)
{
cloneLayer.style.pixelLeft -= stepX;
cloneLayer.style.pixelTop -= stepY;
if(cloneLayer.style.pixelLeft < orgnX)
{
stepX = 1;
}
if(cloneLayer.style.pixelTop < orgnY)
{
stepY = 1;
}
if(cloneLayer.style.pixelLeft == orgnX)
{
stepX = 0;
}
if(cloneLayer.style.pixelTop == orgnY)
{
stepY = 0;
}
if(cloneLayer.style.pixelLeft == orgnX && cloneLayer.style.pixelTop == orgnY)
{
EndMove();
}
}
//to the design
else
{
EndMove();
}
}
//stop and then back to the state ()carton
function EndMove()
{
sourceLayer.style.pixelLeft = orgnX;
sourceLayer.style.pixelTop = orgnY;
cloneLayer.style.pixelLeft = orgnX;
cloneLayer.style.pixelTop = orgnY;
cloneLayer.className = "docked";
clearInterval(back);
}
//Main function of this demo
function startDraging(inAim,inSource,inClone,initAimX,initAimY,initOrgnX,initOrgnY)
{
getLayer(inAim,inSource,inClone)
initDrag(initAimX,initAimY,initOrgnX,initOrgnY);
sourceLayer.onmousedown = BeforeDrag;
document.onmousemove = OnDrag; //if we use cloneLayer,then the content will be draged ,and well a bug
cloneLayer.onmouseup = EndDrag;
}
//transfer
startDraging("aim","sourceLayer","cloneLayer",600,500,50,50);
//-->
</script>
</body>
</html> As beginning,still don't know how to begin this post~but really should begin to write something about tech.Sometimes some one like to talk that A good bazoo can
make a good bussiness card~,may be so,may be not,I really want to throw it away,but cannot,just can to learn and write ...off topics...
From now I propose to write a route about js tech with my learning steps.Ajax is a good thing maybe the world just like it some good but really short. Js is called for the full JavaScriptlanguage,it's welcomed because it can be run at the piont of client and also effective.when we talk about java object orientation is the focus piont which attracts our attention.we can define a class and then a function.But to JS ,when we create a function and make a instance of this function,we just regard it as a class,so we may say a newable function was a class.The class may have its own attributes or methods,but now we can use the sign["~"]to quote it(refer to).
Now I give a simple example about this
Of course,u can express with arr.push("Eric").After this,I want to say,we can insert,update & delete attributes or methods when we need.As following
user.name="Eric";
user.age="21";
user.sex="male";
//we insert 3 Atts/
user.show=function(){
alert("Name is"+this.name);
}
//we insert 1 Mes/
delete is very very easy,a old story.. we use undefined
user.name=undefined;
user.show=undefined;
//....
This is js today's post,easiness goes,.~ ..
FileWriter fw = new FileWriter(writetext);//就像創建打印機
PrintWriter pw = new PrintWriter(fw);//這個呢打印針頭了。


第三步,輸入啟動類。輸入帶有 main 方法的類名
| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
27 | 28 | 29 | 30 | 1 | 2 | 3 | |||
4 | 5 | 6 | 7 | 8 | 9 | 10 | |||
11 | 12 | 13 | 14 | 15 | 16 | 17 | |||
18 | 19 | 20 | 21 | 22 | 23 | 24 | |||
25 | 26 | 27 | 28 | 29 | 30 | 31 | |||
1 | 2 | 3 | 4 | 5 | 6 | 7 |
常用鏈接
留言簿(10)
隨筆分類(95)
- Data Structure && Algorithm (14)
- IBM Tech(8)
- No Category(11)
- Personal Comments(12)
- python(2)
- Simple Java(31)
- SUN Tech(17)
隨筆檔案(97)
- 2009年9月 (1)
- 2008年8月 (1)
- 2008年7月 (2)
- 2008年4月 (3)
- 2008年2月 (2)
- 2008年1月 (1)
- 2007年12月 (3)
- 2007年11月 (3)
- 2007年10月 (7)
- 2007年8月 (2)
- 2007年7月 (5)
- 2007年6月 (1)
- 2007年5月 (8)
- 2007年4月 (15)
- 2007年3月 (9)
- 2007年2月 (2)
- 2007年1月 (3)
- 2006年12月 (6)
- 2006年11月 (2)
- 2006年10月 (5)
- 2006年8月 (2)
- 2006年7月 (5)
- 2006年5月 (9)
文章檔案(10)
相冊
J2ME技術網站
java技術相關
mess
搜索
最新評論

- 1.?re: SWT JFACE .TreeViewer Expand事件及其節點處理方法[未登錄]
-
@求助
非常感謝 解決了問題 - --huhu
- 2.?re: The Fifth Discipline Peter M. Senge(彼得·圣潔)
-
@Scott
- --bc05002@gmail.com
- 3.?re: Java6 MUstang新特性總結(摘錄)
- Thanks for your sharing.I like it very much, thanks!
- --ed hardy
- 4.?re: F3(轉http://blogs.sun.com/chrisoliver/entry/f3)
- 評論內容較長,點擊標題查看
- --runescape gold
- 5.?re: ideas are just a multiplier of execution(copied)
- 評論內容較長,點擊標題查看
- --James07