If you're using Netscape, you can use LiveConnect to talk to JavaScript
from a Java applet and vice-versa.

In your applet you'll need to do something like this:

import netscape.javascript.*;
import java.util.*;

public class MyApplet extends java.applet.Applet {

   JSObject window;
   Hashtable formHash = new Hashtable;

   public void init() {
        super.init();
        // enable communication with the browser, if necesary
        window = JSObject.getWindow(this);
        // now you can call a JavaScript function with something like:
        // window.call("someJavaScriptFunction", someParameter);
   }

   // add public methods accessible to JavaScript in HTML page...
   public void setFormData(String elementName, String elementValue) {
        formHash.put(elementName, elementValue);
        System.err.println(elementName + ": " + elementValue);
   }
        
}



Then, in your HTML page, you'll need some JavaScript to talk to the
applet:

<HTML>
<HEAD>
<script language="javascript">

function sendFormData() {
   // assuming you only have one applet in the page...
   var client = document.applets[0];
   for (var i=0;i<document.forms[0].elements.length;i++) {
        client.setFormData(document.forms[0].elements[i].name,
                document.forms[0].elements[i].value);
   }    
}

</script>
</HEAD>
<BODY>
<FORM name="f1">
<input type=text name="t1" size=10><BR>
<input type=text name="t2" size=10><BR>
<input type=text name="t3" size=10><BR>
<input type=button name="b1" value="Send Data To Applet"
onClick="sendFormData();"><BR>
</FORM>
<APPLET name="app1" code="MyApplet.class" width=0 height=0 MAYSCRIPT>
</BODY>
</HTML>

When you javac the applet, you'll need to have the netscape classes in
your classpath (i.e. /your/netscape/path/java/classes/java40.jar).

You can test the code above by viewing the Java Console in Netscape
when you submit the form.  Hope this helps.

-Rob


On Thu, 14 Jan 1999 [EMAIL PROTECTED] wrote:

> can java applet read and write data form or to a form of HTML?
> How to do? Please tell me.thank
> 

Reply via email to