2008年(3500)
分类:
2008-05-04 20:09:01
第一步,编写需要传递的对象类 /** * Class Person just a demo for translate this class to client * @author: rookie * @datetime: 2002-7-26 */ package exapplet; import java.io.*; public class Person implements Serializable {//必须实现Serializable接口才能序列化 private String m_Name; private int m_Age; public Person() { this("",0); } public Person(String name,int age) { this.m_Name = name; this.m_Age = age; } public String getName() { return m_Name; } public int getAge() { return m_Age; } //Serializable接口中声明的方法 private void writeObject(java.io.ObjectOutputStream out) throws IOException { out.defaultWriteObject(); } //Serializable接口中声明的方法 private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); } public static void main(String[] args) { try { Person p = new Person("Liaoyuan",25); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("test.obj"))); oos.writeObject(p); oos.close(); ObjectInputStream ois = new ObjectInputStream (new FileInputStream(new File("test.obj"))); Person q = (Person)ois.readObject(); ois.close(); System.out.println(q.getName()); System.out.println(q.getAge()); } catch(Exception exp) { exp.printStackTrace(); } } } 第二步,实现Servlet package exapplet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { String fPath = "H:\\XSchool\\WorkingRoom\\exapplet\\src\\java.obj"; String act = req.getParameter("Action"); if ( act.equals("UpdateObject") ) { //创建更新JavaObject Person p = new Person("Liaoyuan",26); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(fPath))); oos.writeObject(p); oos.close(); DataOutputStream dos = new DataOutputStream(res.getOutputStream()); dos.writeBytes("Server Java Object Updated OK!"); dos.close(); } else if ( act.equals("GetObject") ) { //传回对象给Applet res.setContentType("application/octet-stream"); Person p; ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(fPath))); try { p = (Person)ois.readObject(); } catch (ClassNotFoundException e) { p = new Person(); } ois.close(); ObjectOutputStream oos = new ObjectOutputStream(res.getOutputStream()); oos.writeObject(p); oos.close(); } } public String getServletInfo() { return "A simple Servlet!"; } } 第三步,实现呼叫Servlet更新对象的Applet package exapplet; import java.awt.*; import java.applet.*; import java.io.*; import java.net.*; import netscape.javascript.*; public class MyApplet extends java.applet.Applet { public void init() { } public void paint(Graphics g) {} //This method will be call in html public void invoke() { try { URL url = new URL(""); URLConnection urlcon = url.openConnection(); urlcon.connect(); DataInputStream dis=new DataInputStream(urlcon.getInputStream()); String txt = dis.readLine(); dis.close(); String[] info = new String[]{txt}; JSObject.getWindow(this).call("alert",info); } catch (Exception e) { } } } 第四步,实现提取对象的Applet package exapplet; import java.awt.*; import java.applet.*; import java.io.*; import java.net.*; import netscape.javascript.*; public class MyAppletB extends java.applet.Applet { public void init() { } public void paint(Graphics g) {} //This method will be call in html public void invoke() { try { URL url = new URL(""); URLConnection urlcon = url.openConnection(); urlcon.connect(); ObjectInputStream ois=new ObjectInputStream(urlcon.getInputStream()); Person p = (Person)ois.readObject(); ois.close(); String[] info = new String[]{"Person Info\n\n-Name: " p.getName() "\n-Age : " p.getAge()}; JSObject.getWindow(this).call("alert",info); } catch (Exception e) { } } } 第五步,配制WebServer(我用的是Tomcat4.0) 拷贝编译后的MyServlet.class到相应目录下, 编辑对应的web.xml文件,确保下面的内容正确中间有错误的地方请指教,zlyperson@163.net 下载本文示例代码第六步,编写html文件 呼叫更新的html PostToApplet exapplet.MyServlet PostToApplet /exapplet/PostToApplet New Document For Using Applet Call Servlet Update Java Object Hello This is a Test!
提取对象的htmlNew Document For Using Applet Hello This is a Test!