Accessing Native methods from a Java Applet

www.ebsolutech.com

 

by Sanket Bakshi, s.bakshi@ebsolutech.com

 

Previous section: Signing The JAR Files

Web Page to Applet Communication

o       Normal Scenario

o       Problems with the Java Plug-in

Normal Scenario

Normally, web page to applet communication can be carried out by following two methods –

o       Passing parameters to the applet

o       Calling Applet functions from JavaScript.

Passing parameters to the applet

The first approach can be easily achieved by passing the parameters to the applet at the time of loading it. The <PARAM> tag can be used for the same purpose. The <PARAM> tag accepts the name and the value for a parameter as follows –

<APPLET CODE=”xzy.class” width=”100” height=”100”>

<PARAM NAME=”name” VALUE=”value”>

</APPLET>

The value passed by such a tag can be retrieved in the Applet using the getParameter(String) in the following manner –

String param = getParameter(“name”);

The parameter passed to the getParameter() function is the name of the parameter for which the value is requested. The returned value is the value of specified parameter in a String.

Calling Applet functions from JavaScript

The above approach can be used when parameters are passed to an applet when it is loaded. In that case, the parameters can be received and even processed on. But in many cases, a return value is required on the web page to display to the user or might be even any other purpose. This can be achieved by directly calling the applet functions.

The JavaScript can be used to call the methods form the Applet. The values returned in this case are directly returned to JavaScript variables and can be therefore used in the web page.

Problems with the Java Plug-in

The two above mentioned approaches are possible while communicating from JavaScript to an Applet. While we use the plug-in, the web page is converted using the HTML converter so as to include the <OBJECT> and <EMBED> tags. These tags define a content type as application/x-java-applet;version=1.3 with the TYPE parameter. The Java plug-in comes in the scene when this type is encountered. Both <OBJECT> and <EMBED> tags allow passing of parameters to the applet using the <PARAM> tag. So the first approach of passing parameters to applets when it is loaded is possible even when plug-in is used.

In the second case, to return the values back to the page, the <EMBED> tag does not allow calling the functions. This is because basically the <EMBED> tag embeds the Java Plug-in into the page. And then, the plug-in uses its own JRE to run the applet. Due to this, it becomes completely legitimate for the <EMBED> tag to disallow calls to any functions of the embedded control. Parameters can still be passed and therefore the first approach is possible.

The <OBJECT> tag on the other hand, creates an ‘object’ of specified ActiveX control or a COM component. Therefore, methods from the applet can be called using the <OBJECT> tag.

This approach is considered as a problem because the objective of the document is to document a method, which runs successfully on both the browsers. This approach can be used only on the Internet Explorer and that is any it will not be used.

Next section: Applet to Web Page communication
Table of Contents


Top of Page | Home


© 2001 – EBSolute Technologies