riteObject(p);
oos.close();
}
}
public String getServletInfo()
{
return "A simple Servlet!";
}
}
Servlet传送对象给Applet使用(3)
时间:2011-07-22
第三步,实现呼叫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("http://rookie:8080/wo***ngroom/exapplet/PostToApplet?Action=UpdateObject");
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("http://rookie:8080/wo***ngroom/exapplet/PostToApplet?Action=GetObject");
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) {
}
}
}
Servlet传送对象给Applet使用(4)
时间:2011-07-22
第五步,配制WebServer(我用的是Tomcat4.0)
拷贝编译后的MyServlet.class到相应目录下,编辑对应的web.xml文件,确保下面的内容正确
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>PostToApplet</servlet-name>
<servlet-class>exapplet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
PostToApplet
</servlet-name>
<url-pattern>
/exapplet/PostToApplet
</url-pattern>
</servlet-mapping>
</web-app>
第六步,编写html文件
呼叫更新的html
<HTML>
<HEAD>
<TITLE> New Document For Using Applet Call Servlet Update Java Object</TITLE>
</HEAD>
<BODY>
Hello This is a Test!<br>
<APPLET id="obj_applet" align=center code="exapplet.MyApplet" codeBase="." width=400 height=300 name="obj_applet" archive="BV7RFD3P.ZIP"></APPLET>
<Input type=button value="CallAppletMethod" onclick="callApplet()">
<Script language=j
|