How to pass values from HTML page to java applet?

前端 未结 2 1912
孤独总比滥情好
孤独总比滥情好 2020-12-10 20:56

I have tried passing values using javascript like below



        
相关标签:
2条回答
  • 2020-12-10 21:15

    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.

    0 讨论(0)
  • 2020-12-10 21:42

    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();
    
    0 讨论(0)
提交回复
热议问题