proves connexió javascript - java

codi font: sortida.class




introtext:            envia dades a applet

 

http://www.rgagnon.com/masters/java-js.html
Aquí hi han un munt solucions per interconnectar applets i javascript en ambdues direccions, inclós enviar i rebre arrays, que és més segur enviar (en ambudes direccions) com strings separades per '|' i reconstruir-les després com a arrays

Tot seguit, explicació extreta de http://www.rgagnon.com/javadetails/java-0183.html:

JavaScript calling Java (dynamic runtime invocation)

Code for JavaScript to call Java is given below. Invoking Java at runtime like
this is called "LiveConnect" by Netscape. The code below works for
both Netscape
and Internet Explorer.

First Step: Give the applet on the Webpage a Name

Using a name is preferable than referring to applet[0].

<applet code="JSTellJava.class" name="Name1" width=320
height=220>
<param name = "text" value = "Ready to Begin">
</applet>

Example 1: Call a Java method

To invoke a Java method called "increaseNumber" (from JavaScript),
simply prefix
the public Java method with "document.appletname", as shown below.

var theIncrement;
theIncrement = form1.increment.value;
document.Name1.increaseNumber(theIncrement);

Example 2: Call a Java method and retrieve return value

It is the same as the last example, except the return value is obtained:

str = document.Name1.getString();

Example 3: Access a Public Java Variable

Although it is not good practice to directly manipulate variables in a Java
class, a public Java applet variable called "delay" can be accessed in
a similar
manner:

document.Name1.delay = 500;