已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Applet和JavaScript通信Applet和JavaScript通信1. Applet调用JavaScript中的方法Applet调用JavaScript中的方法,主要是通过相应的JSObject方法来获得相关的DOM对象(例如document, form Etc),然后通过eval()来调用相应的方法。下面是一个简单的实例。用户输入相应的姓名,利用applet显示问候语。/MyApplet.htmlENTER YOUR NAME:/MyApplet.javaimport java.applet.Applet;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import netscape.javascript.JSObject;import netscape.javascript.JSException;public class MyApplet extends Applet String text = welcome friend.;JSObject window, document, form, textField;Button display;public void init() this.setFont(new Font(,Font.BOLD,19);this.setBackground(Color.pink);this.setForeground(Color.blue);window = JSObject.getWindow(this);document = (JSObject) window.getMember(document);form = (JSObject) document.getMember(form1);textField = (JSObject) form.getMember(name);this.setLayout(new BorderLayout();Panel panel = new Panel();display = new Button(DISPLAY);display.addActionListener(new ButtonEventHandler();panel.add(display);this.add(BorderLayout.SOUTH,panel);public void paint(Graphics g) g.drawString(text, 120, 50);class ButtonEventHandler implements ActionListener public void actionPerformed(ActionEvent event) String s = event.getActionCommand();if (DISPLAY.equals(s) text = Hi + (String) textField.getMember(value)+ ,Have a nice day.;window.eval(alert(please smile,it is good for your health);repaint(); 注:compile when using the netscape.javascript.JSObject package For Java 1.4.2 and later: add plugin.jar to your classpath. It can be found in the lib directory of your JRE installation, e.g. C:Program FilesJavajre1.5.0libplugin.jar 2. Browsers JS calls Applet This example is to show that JavaScript calls methods of a Java applet. Inside the tag of the applet shown below, a name is defined using the name=“app attribute. Then the browsers JavaScript should be able to call methods of the applet by using app.method(). HTML DOM Applet Object Applet Object The Applet object represents an HTML applet element. The applet element is used to place executable content on a page.2. Applet和XML-RPC通信的过程总的来说,是通过在HTML页面中接收参数,然后利用JavaScript调用Applet的方法。在Applet中可以调用XML-RPC client的方法获得相应的处理结果。然后XML-RPC client向XML-RPC server发出请求,调用相关的类处理后将结果返回给XML-RPC client,在返回给applet,然后再利用JavaScript调用Applet的方法,将数据显示在HTML页面。1. Create handler classCreate a class, or a set of classes, which are implementing the remote procedure calls. Heres some example of such a class: Example1:用于简单的数据的计算/Calcaulator.javapackage pvcs;public class Calculator public int add(int i1, int i2) return i1 + i2;public int subtract(int i1, int i2) return i1 - i2;Example2:/用于Dialogue数据的处理,可以将参数信息放在HashMap中,方便传递package pvcs;import java.util.Map;public class DialogueDataProcessing public String dataProcessing(Map map)String str=?;for(int i=0;imap.size();i+)str+=map.get(String.valueOf(i)+?;return str;Example3:/用户获得制定文件目录下的文件和目录信息/ FileProperties.java获得文件的信息package pvcs;import java.io.File;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class FileProperties private String path;private File currentFile;public FileProperties() this.path = ;currentFile = new File(path);public FileProperties(String path) this.path = path;currentFile = new File(path);/get the name of filepublic String getFileName()return currentFile.getName();/get the filesize of filepublic String getFileSize() return new Long(currentFile.length().toString(); /get file midified timepublic String getFileTimeStamp() Calendar date = Calendar.getInstance();SimpleDateFormat dateFormat = new SimpleDateFormat(yyyy/MM/dd);return dateFormat.format(new Date(currentFile.lastModified();/get the path of the filepublic String getDirectory() return currentFile.getPath();/return the file type of a file or folderpublic boolean getFileType() return currentFile.isFile();/获得目录文件的信息/DirectoryProperties.javapackage pvcs;import java.io.File;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Iterator;import java.util.Vector;public class DirectoryProperties File myDir;File contents;Vector vectorList;Iterator currentFileView;File currentFile;String path;public DirectoryProperties() path = new String();vectorList = new Vector();public DirectoryProperties(String path) this.path = path;vectorList = new Vector(); /set the file pathpublic void setPath(String path) this.path = path;/ get the path of a directory or a filepublic String getDirectory() return myDir.getPath();/list all the files and folders of a folderpublic void refreshList() if (this.path.equals()path = c:;myDir = new File(path);vectorList.clear();contents = myDir.listFiles();for (int i = 0; i contents.length; i+) vectorList.add(contentsi);currentFileView = vectorList.iterator();/process the current filepublic boolean nextFile() while (currentFileView.hasNext() currentFile = (File) currentFileView.next();return true;return false;/get the name of the filepublic String getFileName() return currentFile.getName();/get the file size of the filepublic String getFileSize() return new Long(currentFile.length().toString();/get the file modified timepublic String getFileTimeStamp() Calendar date = Calendar.getInstance();SimpleDateFormat dateFormat = new SimpleDateFormat(yyyy/MM/dd);return dateFormat.format(new Date(currentFile.lastModified();/return the fileType of the filepublic boolean getFileType() return currentFile.isFile();2Before talking to an XML-RPC server, you need an instance of XmlRpcClient package pvcs;import .MalformedURLException;import .URL;import java.util.Date;import java.util.HashMap;import java.util.Map;import org.apache.xmlrpc.XmlRpcException;import org.apache.xmlrpc.client.XmlRpcClient;import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;import org.apache.xmlrpc.client.XmlRpcLiteHttpTransportFactory;import org.apache.xmlrpc.client.XmlRpcSunHttpTransportFactory;import org.apache.xmlrpc.client.XmlRpcTransportFactory;public class XmlRpcServiceClient private static final String CONNECTION_URL = :9999;private static XmlRpcClient client; /初始化public static void initXmlRpcClient() XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();try config.setServerURL(new URL(CONNECTION_URL); catch (MalformedURLException e) e.printStackTrace();config.setEnabledForExtensions(true);config.setConnectionTimeout(60 * 1000);config.setReplyTimeout(60 * 1000);client = new XmlRpcClient();client.setTransportFactory(new XmlRpcSunHttpTransportFactory(client);/ set configrationclient.setConfig(config); /用于文件的处理public static String XmlRpcClientForFile(String path) String result = null;Object params = null;try initXmlRpcClient();/ make a regular callparams = new Object path ;result = (String) client.execute(FileRead.getFileDetails, params); catch (XmlRpcException e) e.printStackTrace();return result; /用于数据的计算public static int XmlRpcClientForCalculator(Map map) Integer result = null;Object params = null;try / create configrationinitXmlRpcClient();/ make a regular callparams = new Object Integer.parseInt(String) map.get(param1),Integer.parseInt(String) map.get(param2) ;result = (Integer) client.execute(Calculator.add, params); catch (XmlRpcException e) e.printStackTrace();return result; /用于Dialogue数据的处理public static String XmlRpcClientForDialogue(Map map)throws MalformedURLException String result = ;Object params;try / create configrationinitXmlRpcClient();/ make a regular callparams = new Object map ;result = (String) client.execute(DialogueDataProcessing.dataProcessing, params); catch (XmlRpcException e) e.printStackTrace();return result;3. Once that is done, create an instance of WebServer/用于服务器段数据的处理,真正的handler,需要配置相关的properties文件package pvcs;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import org.apache.xmlrpc.XmlRpcException;import org.apache.xmlrpc.server.PropertyHandlerMapping;import org.apache.xmlrpc.server.XmlRpcServer;import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;import org.apache.xmlrpc.webserver.WebServer;public class XmlRpcServiceServer private static final int port = 9999;public static void initXmlRpcServer() / create an instance of WebServerWebServer webServer = new WebServer(port);XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();PropertyHandlerMapping phm = new PropertyHandlerMapping();try / add handler to process/ phm.addHandler(FileRead, pvcs.getFileProperties.class);phm.load(Thread.currentThread().getContextClassLoader(),pvcs/MyHperties);xmlRpcServer.setHandlerMapping(phm);XmlRpcServerConfigImpl serverConfig = new XmlRpcServerConfigImpl();serverConfig.setEnabledForExtensions(true);serverConfig.setContentLengthOptional(false);System.out.println(starting the server.);webServer.start(); catch (XmlRpcException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();public static void main(String args) throws XmlRpcException, IOException initXmlRpcServer();4.相关的属性文件的配置,配置的相关的内容如下/MyHpertiesFileRead=pvcs.GetFilePropertiesCalculator=pvcs.CalculatorDialogueDataProcessing=pvcs.DialogueDataProcessing5.Applet和XML-RPC Client通信/ MyApplet.javapackage pvcs;import java.applet.Applet;import java.awt.Graphics;import java.io.IOException;import .MalformedURLException;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.Vector;import org.apache.xmlrpc.XmlRpcException;public class MyApplet extends Applet private String transformedData =welcome; private String path = ; private String xmlData=xmlData; private String methodName; private Vector vector; private int CalculatorResult; /Dialogue processing parameter private String dialOutput=; /accept the path string from the userpublic void setFilePath(String str) this.path = str; /get the data about the filespublic String getData() transformedData = XmlRpcServiceClient.XmlRpcClientForFile(path);return transformedData;public void Compute(String xmlData) this.xmlData=xmlData;/可以将相关的参数信息封装在Map中Map map=new GetCalculatorResult().readXmlData(xmlData);CalculatorResult=XmlRpcServiceClient.XmlRpcClientForCalculator(map);public
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工程项目进度计划编制及调整实例
- 六年级美术课堂主题教学设计
- 牛津译林六年级英语期中提分训练
- 中小学体育课程教学改进方案
- 河道清淤工程技术方案
- 建筑项目安全风险预警与控制措施
- 工地环境保护管理责任制
- 钢结构施工现场安全管理操作手册
- 小学一年级语文复习课教学设计
- 企业数字营销策略实战手册
- 2025年国家普通话水平考核测试标准试卷(共20套)
- 护理重点专科汇报
- 合同签订培训课件
- 诊疗技术规范与医疗操作规程
- 小学班主任经验交流课件
- 人教版七年级上册历史全册思维导图
- 宫颈癌术后护理
- 西师版五年级上册(全册)数学【知识点详细梳理】
- 自然地理与资源环境专业生涯发展展示
- 重度贫血病例讨论
- 高考英语读后续写专题 02 话题分类+公益组织、公益活动(人与社会)
评论
0/150
提交评论