Click to See Complete Forum and Search --> : java applet


nishi
05-24-2005, 02:54 AM
hi. how can i capture data from an html page and sent the data to a java applet.

ccoder
05-24-2005, 09:23 AM
You can use JavaScript to pass data to/from the appropriate methods in the applet. It is no different than calling a JavaScript function.

nishi
05-25-2005, 02:18 AM
Can you give me a simple code example dealing with the above issue?

ccoder
05-25-2005, 08:44 AM
Here are some snippets of code from a web page that opens a socket connection to an ANSI C server. The JavaScript statement contains several nested calls, one to each of the public methods in the applet.

showResult(document.commApplet.getResult(document.commApplet.send2Host(server, port, nodeObj[nindx].text,
tblObj[cindx].svr, cmdstr, tblObj[cindx].preq + cmdObj.options[cindx].value)));


// -------------------------------------------------------------------------
// getResult returns the result of the last command executed
// -------------------------------------------------------------------------

public String getResult(int result)
{

return ((result == STATUSOK)?cmdReply.resultStr:errMsg);

}

// -------------------------------------------------------------------------
// send2Host is called every time the user clicks the 'Submit' button - it
// builds the request record, performs the socket communication and sends
// the result (STATUSOK or STATUSFAILED) back to the web page
// -------------------------------------------------------------------------

public int send2Host(String server, int port,
String node, String owner, String cmd, String cmdcode)
{
// remaining code excluded to keep it brief

Does this help?

7stud
05-28-2005, 02:00 PM
hi. how can i capture data from an html page and sent the data to a java applet.
You can send data from an html page directly to an applet using javascript. Try this:
<html>
<head><title></title>

<script type="text/javascript" language="javascript">
<!-- Hide from browsers without javascript

function myFunc()
{
var data = document.getElementsByName("toJava");

//Using javascript to assign values to Java arrays
//works in FF1.0 but not IE6. So, need separate variables in
//in the Applet:

document.applets[0].htmlData1 = data[0].value;
document.applets[0].htmlData2 = data[1].value;

document.applets[0].redraw();

return false; //cancels form submission to server
}

// End hiding -->
</script>
</head>
<body>

<form name="f" method="post" action="" onsubmit="return myFunc()">
<div><input name="toJava" type="text" id="tb1"/></div>
<div><input name="toJava" type="text" id="tb2"/></div>
<div><input type="submit" id="b0" name="b0" value="submit" /></div>
</form>

<applet code="JavascriptToJavaApplet" height = "150" width="200">
</applet>

</body>
</html>

import java.awt.*;
import java.applet.*;

public class JavascriptToJavaApplet extends Applet
{
//Assigning values to Java arrays works in FF1.0 but not IE6:
//public String htmlData[];

public String htmlData1;
public String htmlData2;
private boolean start;

public void init()
{
htmlData1 = new String();
htmlData2 = new String();
start = true;
}

public void paint(Graphics g)
{
//Demonstration showing the data arrived safely:

if (start) //for when the applet first loads in the window
{
g.drawString("Hello from Mr. Applet!", 20, 20);
start = false;
}
else
{
g.drawString(htmlData1 + " " + htmlData2, 20, 20);
}

}

//called by javascript when submit button on the html form is clicked:
public void redraw()
{
repaint();
}
}

nishi
05-30-2005, 04:55 AM
i have tried a similar code as you have given and the page works fine when opened by double clicking it. in fact my applet is a client program and i have a server program in java. i have used Java Rmi for my client/server to communicate.
when am opening the page as follows:
http://localhost/folder/password.html
the applet is not connecting to the java server pogram.

lkehoe
06-30-2005, 09:46 AM
Ccoder-

Would you please give the full code for the socket example(the Java class and the html file)?
I have to do a very similar job and this would be very helpful.
Thanks!