这篇文章是继<<Servlet传送对象给Applet使用>>后,征对网友的特殊问题写的又一个实例.
现发表出来与大家分享,文中瑕癖之处甚多,望各位指教.
这个问题在csdn上已经有讨论了http://www.csdn.net/expert/topic/971/971710.xml.
具体的需求就不讲了,这里把我的实现步骤详细写下,与<<Servlet传送对象给Applet使用>>中提到的方法
一样.用到的CachedRowSet可以到
http://java.sun.com//Download4?userid=zlyperson&platform=zip&button=continue&config-file=rowset-1_0-ea4.config
去下.还有一个netscape的包,在C:\WINNT\java\Packages\0S0W0NFX.ZIP中.
我也把所需要的包及我的实现代码打成了包,其中包扩了CachedRowSet&netscape&本文.
http://63.210.240.215/rookieport/rowset.jar
下面给出全部代码和配置,其中Applet部分涵盖
了Javascript与Applet的互相调用,对于该部分不感兴趣的朋友可以复略.
<pre>
第一步,实现Servlet
package exapplet;
import java.io.*;
import java.sql.*;
import javax.sql.*;
import sun.jdbc.rowset.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet
{
static {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
try {
Connection dbconn = DriverManager.getConnection("jdbc:odbc:BlueSite");
Statement stmt = dbconn.createStatement();
String dbsql = req.getParameter("DBSQL");
ResultSet dbrest = stmt.executeQuery(dbsql);
CachedRowSet crs = new CachedRowSet();
crs.populate(dbrest);
dbrest.close();
stmt.close();
dbconn.close();
file://传回对象给Applet
res.setContentType("application/octet-stream");
ObjectOutputStream oos = new ObjectOutputStream(res.getOutputStream());
oos.writeObject(crs);
oos.close();
} catch (Exception exp) {
exp.printStackTrace();
}
}
public String getServletInfo()
{
return "A simple Servlet!";
}
}
第二步,实现提取对象的Applet
package exapplet;
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import javax.sql.*;
import sun.jdbc.rowset.*;
import netscape.javascript.*;
public class MyAppletB extends java.applet.Applet
{
public void init()
{
}
public void paint(Graphics g)
{}
file://This method will be call in html
public void invoke() {
try {
URL url = new URL("http://Liaoyuan:8080/WorkingRoom/exapplet/PostToApplet?DBSQL=Select%20TopicName,TopicName,TopicName%20from%20Topic");
file://注意url中的空个用%20替代.
URLConnection urlcon = url.openConnection();
urlcon.connect();
ObjectInputStream ois=new ObjectInputStream(urlcon.getInputStream());
CachedRowSet crs = (CachedRowSet)ois.readObject();
ois.close();
while ( crs.next() ) {
JSObject.getWindow(this).call("alert",new String[]{crs.getString(1)});
}
crs.close();
} catch (Exception e) {
}
}
}
第三步,配制WebServer(我用的是Tomcat4.0)
拷贝编译后的MyServlet.class到相应目录下,或者下载我打的包到WEB-INF\lib下
编辑对应的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 </TITLE>
</HEAD>
<BODY>
Hello This is a Test!<br>
<APPLET id="obj_applet" align=center code="exapplet.MyAppletB" codeBase="." width=400 height=300 name="obj_applet" archive="BV7RFD3P.ZIP"></APPLET>
<Input type=button value="CallAppletMethod" onclick="callApplet()">
<Script language=javascript>
function callApplet()
{
obj_applet.invoke();
}
</Script>
</BODY>
</HTML>
</pre>
中间有错误的地方请指教,[email protected]
本文地址:http://com.8s8s.com/it/it18206.htm