实现Applet与Servlet之间进行通讯的简单实例

Servlet传送查询记录集给Applet

这篇文章是继<<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

packageexapplet;

importjava.io.*;

importjava.sql.*;

importjavax.sql.*;

importsun.jdbc.rowset.*;

importjavax.servlet.*;

importjavax.servlet.http.*;

publicclassMyServletextendsHttpServlet

{

static{

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}catch(Exceptione){

e.printStackTrace();

}

}

publicvoiddoGet(HttpServletRequestreq,HttpServletResponseres)

throwsServletException,IOException

{

try{

Connectiondbconn=DriverManager.getConnection("jdbc:odbc:BlueSite");

Statementstmt=dbconn.createStatement();

Stringdbsql=req.getParameter("DBSQL");

ResultSetdbrest=stmt.executeQuery(dbsql);

CachedRowSetcrs=newCachedRowSet();

crs.populate(dbrest);

dbrest.close();

stmt.close();

dbconn.close();

//传回对象给Applet

res.setContentType("application/octet-stream");

ObjectOutputStreamoos=newObjectOutputStream(res.getOutputStream());

oos.writeObject(crs);

oos.close();

}catch(Exceptionexp){

exp.printStackTrace();

}

}

publicStringgetServletInfo()

{

return"AsimpleServlet!";

}

}

第二步,实现提取对象的Applet

packageexapplet;

importjava.awt.*;

importjava.applet.*;

importjava.io.*;

importjava.net.*;

importjavax.sql.*;

importsun.jdbc.rowset.*;

importnetscape.javascript.*;

publicclassMyAppletBextendsjava.applet.Applet

{

publicvoidinit()

{

}

publicvoidpaint(Graphicsg)

{}

//Thismethodwillbecallinhtml

publicvoidinvoke(){

try{

URLurl=newURL("http://Liaoyuan:8080/WorkingRoom/exapplet/PostToApplet?DBSQL=Select%20TopicName,TopicName,TopicName%20from%20Topic");

//注意url中的空个用%20替代.

URLConnectionurlcon=url.openConnection();

urlcon.connect();

ObjectInputStreamois=newObjectInputStream(urlcon.getInputStream());

CachedRowSetcrs=(CachedRowSet)ois.readObject();

ois.close();

while(crs.next()){

JSObject.getWindow(this).call("alert",newString[]{crs.getString(1)});

}

crs.close();

}catch(Exceptione){

}

}

}

第三步,配制WebServer(我用的是Tomcat4.0)

拷贝编译后的MyServlet.class到相应目录下,或者下载我打的包到WEB-INF\lib下

编辑对应的web.xml文件,确保下面的内容正确

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.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>NewDocumentForUsingApplet</TITLE>

</HEAD>

<BODY>

HelloThisisaTest!<br>

<APPLETid="obj_applet"align=centercode="exapplet.MyAppletB"codeBase="."width=400height=300name="obj_applet"archive="BV7RFD3P.ZIP"></APPLET>

<Inputtype=buttonvalue="CallAppletMethod"onclick="callApplet()">

<Scriptlanguage=javascript>

functioncallApplet()

{

obj_applet.invoke();

}

</Script>

</BODY>

</HTML>

</pre>

相关推荐