如何从HTML页面值传递给Java小程序? [英] How to pass values from HTML page to java applet?

查看:197
本文介绍了如何从HTML页面值传递给Java小程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试过使用JavaScript值象下面这样

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>

警报框显示在屏幕上输入的值,但该applet code未显示相同。我的小程序code是

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);
    }
}

谁能告诉我的code传递和检索值和参数标记的HTML?

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

推荐答案

我觉得你可以指定像这样的JavaScript对象的参数:

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>

不过,我不知道与IE兼容的,所以你可能要文件撰写你的小程序code注入像这样的各参数值

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>

从Java发送POST

我在我的评论说,这是一个比较复杂的。你不得不POST(您也可以使用GET),你的价值观,以托管文件(可以是任何服务器端脚本技术)。下面说明了这一点, code取自这里

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();

这篇关于如何从HTML页面值传递给Java小程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆