我尝试使用下面的javascript传递值
<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代码是
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);
}
}
有人能告诉我传递和检索param标签到html标签的值吗?
答案 0 :(得分:4)
我认为您可以从javascript对象中指定您的参数,如下所示:
<applet code="Calc.class" width="100" height="100">
<param name="number" id="parm" value="&{num};">
</applet>
但是,我不确定与IE的兼容性,因此您可能需要{app}代码注入相应的参数值document.write
,如下所示:
<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 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();
答案 1 :(得分:4)
例如: 这是您的小程序代码:
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);
}
}
然后在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>
注意: DrawStringApplet是applet名称; Message是发送到applet的参数;然后,小程序将显示:Howdy, there!
。