Applet Communication
Hi,
I have a form in the applet. I don't know how do I collect information and make applet send information back to server? Also in the form there is a dropdown select box, I need to populate that, so how do I send that information to applet when it gets loaded.
I all, I know and understand applets but I don't know how they can communicate.
Any examples? Any links?
Thanks
[423 byte] By [
ashi70] at [2007-9-26 4:41:12]

Hehe good news, for I think I know EXACTLY the solution you are looking for! :-) Having this problem myself in the past, I devised a method for passing variables to a server through the use of GET requests and server-side scripts.
What I recommend for your needs in exactly the same technique. Please review the following two scripts (the first in Java, the second in Perl [ www.perl.com ]) and extract any code snippets that you may need. Enjoy!
Regards,
Brad Galiette
(While on the subject, if you also have the requirement for your server-side script to return information to the applet, please contact me for a slight addition to both scripts.)
<PRE>
import java.net.*;
import java.awt.*;
class JavaFile1 extends java.applet.Applet{
Label status = new Label("Complete the Form");
TextField textValue = new TextField();
FlowLayout dLayout = new FlowLayout();
Choice choiceList = new Choice();
Button submitData = new Button("Submit");
URL transUrl;
URLEncoder ue = new URLEncoder();
URLConnection servConnect;
public void init(){
//Retrieve list items as parameters on the host HTML form. Parameters,
//in this scnario, should be in the form of:
//
// <APPLET ... >
//<PARAM NAME = "listOptions0" VALUE = "Whose On First">
//<PARAM NAME = "listOptions1" VALUE = "Whats On Second">
//<PARAM NAME = "listOptions2" VALUE = "I Don't Know's On Third">
//...
// </APPLET>
//
//(Note - There is no limit to the number of elements that can exist
//in the Choice list provided that the first parameter is
//"listOptions0", and ever subsequent parameter follows at increments
//of one. Furthermore, the items shall appear in the order
//that there "NAME" value specifies (e.g., "listOptions0" first,
//followed by "listOptions1", etc.)
//
int loInc = 0;
while (getParameter("listOptions" + loInc) != null){
choiceList.addItem(getParameter("listOptions" + loInc));
loInc++;
}
//Display the components in a flow layout
setLayout(dLayout);
add(textValue);
add(choiceList);
add(submitData);
}
//Send the data to the host via a GET request and/or
void submitData(){
//Encode the URL in the "transUrl" URL object
try{
transUrl = new URL(ue.encode("http://myserver/cgi-bin/my_form.cgi?text_field=" + textValue.getText() + "&choice_value=" + choiceList.getSelectedItem()));
} catch(MalformedURLException e){}
//Be aware that there are two ways to go about submitting data to
//a server-based script. In the first, only the data is sent
//via an internal GET request (note that this does NOT transfer
//the user to a different web page); in the second, the information
//is submitted via an external GET request thus causing the page
//to change entirely. Depending upon your needs, it is important
//to know both techniques.
//Example 1
status.setText("Sending Information...");
try{
servConnect = this.serverlet.openConnection();
servConnect.connect();
} catch(IOException e){}
status.setText("Information Sent!");
//Example 2 (This has been commented out to permit for code accuracy;
//if you are in need of changing the page in which your user is
//located, uncomment this code and comment-out the code in "Example 1"
//
// getAppletContext().showDocument(transUrl);
}
//Handle the action of the "Submit Data" button in such a way so that
//submitData() is called.
public boolean action(Event evt, Object arg){
if (evt.target instanceof Button){
String lbl = (String)arg;
if (lbl.equals("Submit")){
submitData();
}
}
}
}
#!/usr/bin/perl
#A library for retrieving GET and POST variables (download at http://cgi-lib.berkeley.edu/ )
require "cgi-lib.pl";
#Retrieve the actual variables; all incoming variables can be accessed via $in{"name_of_variable"}
&ReadParse();
#Print the variables to a file (in practice, you can do anything you desire with the variables received but
#very often, they shall be printed to a file or passed to a database). (Note - The file is being opened in "append"
#mode so that all new additions shall be added to the end of the file. Also, observe that the variables are
#being separated by a "|"; this is a sufficient data separator, but when used for real-world applications,
#make SURE that you filter data to eliminate the "|" [Tip - substitute all instances of "|" with "¦", as
#the latter is the HTML-code equivalent of "|"; a simple command for doing this is $myvar=~s/\|/¦/ig;
#assuming, of course, that $myvar is the variable of your data.])
open(DBFILE, ">>dbfile.txt");
print DBFILE $in{"text_field"} . "|" . $in{"choice_value"} . "\n";
close(DBFILE);
</PRE>