3

I have tried passing values using javascript like below

<script language= "javascript" type= "text/javascript">
var num;
function getVal()
{
num=document.getElementById('in').value;
alert(document.getElementById('parm').value);
}
</script>
<body>
<form >
Number : <input type="text" id="in"  ><br/>
<button id="myBtn" onclick="getVal()">Try it</button><br/>  
</form>
<APPLET code="Calc.class" width="100" height="100">
<PARAM name="number" id="parm">
</APPLET>

</body>    
</html>

The alert box displays the entered value on screen but the applet code is not displaying the same. My applet code is

public class Calc extends Applet
{

    private String strDefault = "Hello! Java Applet.";
    public void paint(Graphics g) {
    String strParameter = this.getParameter("number");
    if (strParameter == null)
    strParameter = strDefault;
    g.drawString(strParameter, 10, 10);
    }
}

Can anyone tell me the code to pass and retrieve values to and from param tag to html?

prgmrDev
  • 910
  • 1
  • 10
  • 20

2 Answers2

4

I think you can specify your parameters from javascript objects like so:

<applet code="Calc.class" width="100" height="100">
    <param name="number" id="parm" value="&{num};">
</applet>

However, I'm not sure of the compatibility with IE so you may have to document.write out your applet code injecting the respective parameter values like so:

<head>
    <script type="text/javascript">
        var num;

        function getVal() {
            num = document.getElementById('in').value;

            writeAppletTags();
        }

        function writeAppletTags() { 
            var container = document.getElementById("applet-container");

            container.innerHTML = "<applet code=\"Calc.class\" width=\"100\" height=\"100\">";
            container.innerHTML += "<param name=\"number\" value=\"" + num + "\">";
            container.innerHTML += "</applet>";
        }
    </script> 
</head>
<body>
    Number : <input type="text" id="in"  ><br/>
    <button id="myBtn" onclick="getVal()">Try it</button><br/>  
    <div id="applet-container" />
</body>

Sending POST from Java

As I said in my comment, this is a little more complicated. You'd have to POST (you could also use GET) your values to a hosted file (can be any server side scripting technology). The following demonstrates this, code taken from here.

URL url;
URLConnection urlConnection;
DataOutputStream outStream;
DataInputStream inStream;

// Build request body
String body = "key=value";

// Create connection
url = new URL("http://myhostedurl.com/receiving-page.php");
urlConnection = url.openConnection();
((HttpURLConnection)urlConnection).setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length", ""+ body.length());

// Create I/O streams
outStream = new DataOutputStream(urlConnection.getOutputStream());
inStream = new DataInputStream(urlConnection.getInputStream());

// Send request
outStream.writeBytes(body);
outStream.flush();
outStream.close();

// Close I/O streams
inStream.close();
outStream.close();
Paul Aldred-Bann
  • 5,840
  • 4
  • 36
  • 55
  • It is printing as it is in drawString method. The alert box displays entered number but the drawString method displays "&{num};". FYI I am using firefox 12.0 on Ubuntu 10.04 – prgmrDev Sep 25 '12 at 14:37
  • @user1220982 try the `document.writeln` amendment I've just made. – Paul Aldred-Bann Sep 25 '12 at 14:40
  • The applet is printing "Undefined" – prgmrDev Sep 25 '12 at 14:48
  • @user1220982 are you making sure you've set your `num` variable to something before writing the applet code? Try initializing your `num` variable at declaration i.e. `num = 10;` and your applet param as `value=\"" + num.toString() + "\"`. – Paul Aldred-Bann Sep 25 '12 at 14:50
  • If I initialize num to 10, applet prints value 10 irrespective of user input. If I do not initialize num to 10, applet prints the default text i.e., "Hello! Java Applet." Which means it is getting null value from applet's param tag. – prgmrDev Sep 25 '12 at 14:55
  • @user1220982 yes because you're not rewriting the parameter html when you change the value of `num` - I left this for you to figure out. I've amended my answer to reflect what I mean. – Paul Aldred-Bann Sep 25 '12 at 15:09
  • I am rewriting it at num = document.getElementById('in').value; line. – prgmrDev Sep 25 '12 at 15:18
  • @user1220982 yes, you are and this works fine. What you're not doing is reflecting this in the rendered html as this was executed on page load (when `num` had no value). So (as you can see in my answer) you need to rewrite the html each time `num` changes value. – Paul Aldred-Bann Sep 25 '12 at 15:28
  • Got it, Thank you. Could you tell me the way to retrieve value back from applet? – prgmrDev Sep 25 '12 at 15:56
  • @user1220982 that's a little more complicated, essentially you'd have to POST / GET the data from your applet to a receiving (hosted) web page either via `Request.Form["key"]` or `Request.QueryString["key"]`. – Paul Aldred-Bann Sep 26 '12 at 08:24
  • Could you please give an example? I am new to this concept – prgmrDev Sep 26 '12 at 13:55
  • I used container.innerHTML = "" +" "+ "" +" "+ ""; to make it work – Mohammed Subhi Sheikh Quroush May 13 '13 at 08:55
4

Source from this article

For example: This is your applet codes:

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

public class DrawStringApplet extends Applet {

  private String defaultMessage = "Hello!";

  public void paint(Graphics g) {

    String inputFromPage = this.getParameter("Message");
    if (inputFromPage == null) inputFromPage = defaultMessage;
    g.drawString(inputFromPage, 50, 25);

  }

}

Then in HTML:

<HTML>
<HEAD>
<TITLE> Draw String </TITLE>
</HEAD>

<BODY>
This is the applet:<P>
<APPLET code="DrawStringApplet" width="300" height="50">
<PARAM name="Message" value="Howdy, there!">
This page will be very boring if your 
browser doesn't understand Java.
</APPLET>
</BODY>
</HTML> 

Notice: DrawStringApplet is you applet name; Message is a parameter sent to applet; Applet will then display: Howdy, there! as a result.

Khanh Tran
  • 447
  • 6
  • 21