Share this page 

Retrieve values from a Java applet for HTML form (CGI)Tag(s): Javascript interaction


Retrieve the value with a Javascript function called via the onSubmit event of the form.

[InitHTMLForm.java]

public class InitHTMLForm extends java.applet.Applet {
   public String getFirstName() {
       // in real life, you have TextField in your Applet and
       // you want to transert its content to the HTML FORM
       // return myTextField.getText();
       return "Real's HowTo";
      }
}

[HTML and Javascript]

<HTML><HEAD>
<SCRIPT>
function getValueFromApplet(){
   document.myForm.q.value = document.myApplet.getFirstName();
   return true;
   }
</SCRIPT>
<BODY>
 <APPLET CODE="InitHTMLForm.class" 
         NAME="myApplet"
       HEIGHT=0 WIDTH=0>
 </APPLET>
 <FORM ACTION="http://www.google.ca/search" 
     NAME="myForm" 
     onSubmit="return getValueFromApplet()">
   <INPUT TYPE="hidden" VALUE="" NAME="q">
   <INPUT TYPE="submit" VALUE="Submit" >
 </FORM>
</BODY></HTML>

Try it here