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]
# 1
I think using applet-servlet communication is the best way.but i have another way also.Create URLConnection object of your script page and pass all variable through url get method.FromNischal
enjoynkp at 2007-6-29 18:03:21 > top of Java-index,Core,Core APIs...
# 2
I still looking for some more ideas on this? Anyone...
ashi70 at 2007-6-29 18:03:21 > top of Java-index,Core,Core APIs...
# 3
When you say Server do you mean Data server or just send info back to a fileserver? (Use a servlet)
morgalr at 2007-6-29 18:03:21 > top of Java-index,Core,Core APIs...
# 4
If you need to send back to a File Server use a servlet.
morgalr at 2007-6-29 18:03:21 > top of Java-index,Core,Core APIs...
# 5
Server I mean webserver... I have to use Applets here.
ashi70 at 2007-6-29 18:03:21 > top of Java-index,Core,Core APIs...
# 6

I dont think I fully understand your problem, but here is my 2 cents worth....

To populate the applet forms:

1

Store the optins for each drop down list/combo box in a flat file. Open a URL connection to that file. Create a BufferedInputStream Reader Object to read in the file a line at a time. Then assign the values accordinly.

2

You can also use a Servlet, but this requires you to re-compile your webserver with Tomcat (if your using an Apache webserver). This adds additional overhead, because Tomcat starts up its own JVM.

3.

You could try using PHP scripts. They are a good alternative to CGI scripts. You could use the PHP script to dynamically create a HTML page to load your applet. the script could read the flat file/database and put the options in the forms into the HTML page as PARAM tags.

Ken.

BrowneK at 2007-6-29 18:03:21 > top of Java-index,Core,Core APIs...
# 7

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>

BGaliette at 2007-6-29 18:03:21 > top of Java-index,Core,Core APIs...