This article is another example following the use of the << servlet transfer object >>, comes from the special problem of netizens, is now published, sharing with everyone, there are many in the literary, I hope all the advice. This problem There is already discussed http://www.971/971710.xml on 9CBS. If the specific needs are not mentioned, here will write my implementation step, and the << servlet transfer The object is the same as the method mentioned in the applet. The cachedrowset used can be used to http://java.sun.com//download4?userid=zlyPerson&platform=zip&button=Continue&config-file=RowSet 1_0-ea4.config Next. There is also a netscape package, in C: /Winnt/java/packages/0S0w0nfx.zip. I also picked the needed package and my realization code, including CachedRowSet & Netscape & this text .http: //63.210.240.215/rookieport/rowset.jar gives all code and configuration below, where the applet part covers the mutual calls of JavaScript and Applets, and the friends who are not interested in the part can be replied.
first, implement servletPackage 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 {type {class.forname ("sun.jdbc.odbc.jdbcodbcodbdriver");} 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 ( " "); Resultset dbrest = stmt.executeQuery (dbsql); cachedrowset (); crs.populate (); dbrest.close (); stmt.close (); dbconn.close (); file: / / return object to the 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!";}} Second step, implement the appletPackage 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 Note that the air was replaced with a 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) {}}}
In the third step, formulate WebServer (I use Tomcat4.0) Copy-compiled myServlet.class to the corresponding directory, or download me to edit the corresponding web.xml file under the web-inf / lib, make sure The content is correct XML Version = "1.0" encoding = "UTF-8"?>
PostToApplet servlet-name> exapplet.myservlet servlet- Class> servlet> PostToApplet servlet-name> / exapplet / postToApplet url-pattern> servlet-maping> web-app> In the fourth step, write HTML
new document for using applet title> head> Hello this is a test!