Microsoft KB Archive/251962

= How to obtain information from an applet and post it to an HTML form =

Article ID: 251962

Article Last Modified on 6/14/2006

-

APPLIES TO


 * Microsoft Java Virtual Machine
 * Microsoft Visual J++ 6.0 Standard Edition

-



This article was previously published under Q251962



SUMMARY
This article describes how to obtain information from an applet and post it to an HTML form so that the data can be submitted to a Web server.



MORE INFORMATION
This sample creates an applet that interacts with Microsoft JScript to process the user's data. The sample also uses hidden form fields so that the user only interfaces with the applet.

Steps to Create Solution
  Create an applet that contains a public function, which returns the information that you want to submit. In the following sample code, an image reference number is returned: import java.applet.*;

public class myApplet extends Applet {  int m_nCurrImage = 6;

public void init {  }

public int getCurrentImage {     return m_nCurrImage; } }                    Create a Web page with an applet inside the form, and hide the field in the form where your applet submits its data. Create a script function called processApplet that populates the hidden field after it receives the data from the applet. Set the onClick function of the form's submit button to execute the processApplet script function.  The preceding steps should return the following code:    </HEAD> <BODY> <form action="/scripts/info.dll" method=GET name="myForm" > <applet code=myApplet.class id=myapplet width=320 height=240 VIEWASTEXT>

<input type=hidden name="appletImage"> <script language=JScript> function processApplet {        parent.myForm.appletImage.value = myapplet.getCurrentImage;

alert("Sent to form: Image Name: " +              parent.myForm.Picture.value +               " Image Number: " +               parent.myForm.appletImage.value); }

Image Name:<input type=text name="Picture">

<input type=submit value="Send following picture." language="JScript" onClick="processApplet;">

</BODY> </HTML> Note The form's action attribute assumes that Info.dll resides on the server, but this is not necessary for the purpose of this sample. </li> After you name the image, press the button to process the form. A dialog box displays the image name and the number that is being submitted with the form.</li></ol>

<div class="references_section">