站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Javascript 专题Java、Java Applet与 JavaScript间的通信

Java、Java Applet与 JavaScript间的通信

摘要: JavaScript
<textarea readonly style="border:none;font-family:Courier New;line-height:150%;width:760px;overflow-y:visible"> <table width="90%" align=center> <tr> <td align=middle>

Java、Java Applet与 JavaScript间的通信

</td></tr></table>
<table width="90%" align=center> <tr> <td vAlign=top> <table width="90%"> <tr> <td>摘 要:本文着重阐述了网页开发中,通过灵活使用从JavaScript语言中访问Java的方法、从JavaScript中访问JavaScript小程序的方法与变量,以及在Java Applet小程序中使用JavaScript等技术,实现这几种网页开发语言的互相补充,以开发更完美的Web应用程序。</td></tr></table> <table width="90%"> <tr> <td></td></tr></table> <table width="90%"> <tr> <td></td></tr></table> <table width="90%"> <tr> <td >JavaScript是用于HTML环境的开发语言,提供了能够响应Web页面事件的脚本,可以完全访问浏览器窗口的各个方面,善于合并HTML、Java Applet小程序、插入件、服务器方程序和其他Web组件,形成完全集成的Web应用程序。而Java是功能强大的著名开发语言,能够支持Web应用程序的开发,用于开发高级Web对象,并提供实现嵌入Web页面的可执行内容,具有更强的编程功能,可以作为JavaScript功能的良好补充,只不过这些功能被限制在有限的浏览器窗口区中。</td></tr></table> <table width="90%"> <tr> <td >Java与JavaScript可以互相补充,以开发功能更完美的Web应用程序。本文归纳了笔者编程中曾使用过的,涉及到有关JavaScript与Java、Java Applet小程序之间互访的问题的一些方法,仅供参考。</td></tr></table> <table width="90%"> <tr> <td>一、从JavaScript中访问Java方法 </td></tr></table> <table width="90%"> <tr> <td >在HTML脚本中可以用JavaScript直接调用Java中的方法,只需要在调用语句前用“java.lang.”加以表示即可。具体语句用法如下例所示:</td></tr></table> <table width="90%"> <tr> <td >java.Lang.System.Out.println(“Hello!”)</td></tr></table> <table width="90%"> <tr> <td>但此种方法只适用于Netscape Navigator,一般不提倡在Web应用程序中使用。</td></tr></table> <table width="90%"> <tr> <td>二、从JavaScript中访问Java Applet小程序中的方法和变量</td></tr></table> <table width="90%"> <tr> <td >通过JavaScript提供的Applet对象,JavaScript代码可以访问Java的变量和方法,具体步骤和用法示例如下。需要注意的是,JavaScript提供的applet对象具有name属性,而没有方法和事件处理器。</td></tr></table> <table width="90%"> <tr> <td>步骤:</td></tr></table> <table width="90%"> <tr> <td>1) 将要访问的Java Applet小程序的方法和变量声明为Public,且必须在Public类中声明;</td></tr></table> <table width="90%"> <tr> <td>2) Java Applet小程序要先装入才能访问,尽管applet对象没有定义onLoad事件,但可以在HTML文档的〈body〉标志中,指定使用Window对象的onLoad事件处理器;</td></tr></table> <table width="90%"> <tr> <td>3) Java Applet小程序可以用JavaScript的applet对象来访问。</td></tr></table> <table width="90%"> <tr> <td>示例:</td></tr></table> <table width="90%"> <tr> <td>“mytest.htm”</td></tr></table> <table width="90%"> <tr> <td></td></tr></table> <table width="90%"> <tr> <td><head></td></tr></table> <table width="90%"> <tr> <td><script language="JavaScript"></td></tr></table> <table width="90%"> <tr> <td>function accessApplet()</td></tr></table> <table width="90%"> <tr> <td>{ setTimeout("changeText('I like JavaScript!')",1000);</td></tr></table> <table width="90%"> <tr> <td>setTimeout("changeText('I like JavaScript and Java!')",2000);</td></tr></table> <table width="90%"> <tr> <td>setTimeout("changeText('I like Java!')",3000);</td></tr></table> <table width="90%"> <tr> <td>setTimeout("accessApplet()",4000);</td></tr></table> <table width="90%"> <tr> <td>}</td></tr></table> <table width="90%"> <tr> <td>function changeText(s)</td></tr></table> <table width="90%"> <tr> <td>{</td></tr></table> <table width="90%"> <tr> <td>window.document.testText.setText(s) </td></tr></table> <table width="90%"> <tr> <td>//此处调用JavaApplet小程序的setText()方法</td></tr></table> <table width="90%"> <tr> <td>//注意,小程序的名字必须为〈applet〉标志 name属性所标识</td></tr></table> <table width="90%"> <tr> <td>}</td></tr></table> <table width="90%"> <tr> <td></script></td></tr></table> <table width="90%"> <tr> <td></head></td></tr></table> <table width="90%"> <tr> <td><body onload="accessApplet()"> </td></tr></table> <table width="90%"> <tr> <td>//通过调用accessApplet()装入小程序</td></tr></table> <table width="90%"> <tr> <td></td></tr></table> <table width="90%"> <tr> <td></td></tr></table> <table width="90%"> <tr> <td></body></td></tr></table> <table width="90%"> <tr> <td></td></tr></table> <table width="90%"> <tr> <td>“Testtext.java”</td></tr></table> <table width="90%"> <tr> <td>import java.applet.*;</td></tr></table> <table width="90%"> <tr> <td>……</td></tr></table> <table width="90%"> <tr> <td>public class Testtext extends Applet</td></tr></table> <table width="90%"> <tr> <td>{ ……</td></tr></table> <table width="90%"> <tr> <td>public void setText(String s) //setText()必须声明为“public”</td></tr></table> <table width="90%"> <tr> <td>{</td></tr></table> <table width="90%"> <tr> <td>text=s;</td></tr></table> <table width="90%"> <tr> <td>repaint();</td></tr></table> <table width="90%"> <tr> <td>}</td></tr></table> <table width="90%"> <tr> <td>}</td></tr></table> <table width="90%"> <tr> <td>三、在Java Applet小程序中使用JavaScript</td></tr></table> <table width="90%"> <tr> <td>Live Connect提供了Java与JavaScript的接口,可以允许在Java Applet小程序中使用JavaScript。具体步骤如下:</td></tr></table> <table width="90%"> <tr> <td>1) 在HTML脚本中使用〈APPLET〉标志的MAYSCRIPT属性,以允许Java Applet小程序访问脚本;</td></tr></table> <table width="90%"> <tr> <td>2) 将netscape. JavaScript包导入Java Applet小程序;</td></tr></table> <table width="90%"> <tr> <td>3) 在Java Applet小程序中用JSObject类的getWindow( )方法创建JavaScript窗口的句柄;</td></tr></table> <table width="90%"> <tr> <td>4) 在Java Applet小程序中用JSObject类的getMember( )方法访问JavaScript对象;</td></tr></table> <table width="90%"> <tr> <td>5) 在Java Applet小程序中用JSObject类的eval( )方法调用JavaScript方法。</td></tr></table> <table width="90%"> <tr> <td>示例:</td></tr></table> <table width="90%"> <tr> <td>“ReadForm. Java”</td></tr></table> <table width="90%"> <tr> <td>import netscape.javascript.JSObject;</td></tr></table> <table width="90%"> <tr> <td>import netscape.javascript.JSException; //可允许在小程序中处理异常事件</td></tr></table> <table width="90%"> <tr> <td>……</td></tr></table> <table width="90%"> <tr> <td>win=JSObject.getWindow(this); // 获取JavaScript窗口句柄,引用当前文档窗口</td></tr></table> <table width="90%"> <tr> <td>doc=(JSObject)win.getMember("document"); // 访问JavaScript对象</td></tr></table> <table width="90%"> <tr> <td >form=(JSObject)doc.getMember("textForm");</td></tr></table> <table width="90%"> <tr> <td >textField=(JSObject)form.getMember("textField");</td></tr></table> <table width="90%"> <tr> <td>text=(String) textField.getMember("value"); //获取文本区的值</td></tr></table> <table width="90%"> <tr> <td>……</td></tr></table> <table width="90%"> <tr> <td>win.eval("alert(\"This alert comes from Java!\")");</td></tr></table> <table width="90%"> <tr> <td>// 调用JavaScript的alert()方法</td></tr></table> <table width="90%"> <tr> <td>……</td></tr></table> <table width="90%"> <tr> <td>“User.htm”</td></tr></table> <table width="90%"> <tr> <td>……</td></tr></table> <table width="90%"> <tr> <td><body></td></tr></table> <table width="90%"> <tr> <td><form name="textForm"></td></tr></table> <table width="90%"> <tr> <td>

Enter text and then click display text:</td></tr></table> <table width="90%"> <tr> <td><input type="text" name="textField" size="20">

</td></tr></table> <table width="90%"> <tr> <td></form></td></tr></table> <table width="90%"> <tr> <td></td></tr></table> <table width="90%"> <tr> <td>//必须使用MAYSCRIPT属性标志</td></tr></table> <table width="90%"> <tr> <td></td></tr></table> <table width="90%"> <tr> <td></body></td></tr></table> <table width="90%"> <tr> <td></td></tr></table> <table width="90%"> <tr> <td> </td></tr></table> <table width="90%"> <tr> <td >附表列举了JSObject类的方法和描述,更详细的说明可以参考MSDN。值得一提的是,本文中程序示例加下划线的部分,要特别引起注意。</td></tr></table> <table width="90%"> <tr> <td></td></tr></table>
</textarea>
↑返回目录
前一篇: 一个javascript脚本写的俄罗斯方块
后一篇: JavaScript:一个鼠标动态跟随文字特效的示例