从JavaScript调用后,Applet URLConnection.connect失败

时间:2011-11-14 12:57:58

标签: java javascript security applet

我有一个带有Java Applet的网站,该applet需要连接到我的服务器。 这适用于JApplets @Override init(),但不适用于我自己的javascript调用函数。

final URL url = new URL(SERVER_URL + action);
System.out.println("url:" + url);
System.out.println("postString: " + postString);
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(!postString.isEmpty()) {
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("Content-Length", Integer.toString(postString.getBytes().length));
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    System.out.println("connecting...");
    connection.connect(); // same with connection.getOutputStream();
    System.out.println("connected");
    ....

网站:

<a href="javascript: document.applet.update();" title="update from server">Update</a>
<applet id="applet" archive="/public/Antucation-0.1.0.jar?random=3765332307555812156" code="de.antucation.controller.Controller.class" width="100%" height="800px">
<param name="colonyId" value="1">
</applet>

输出:

url:http://localhost:9000/applet/showCode
postString: colonyId=1
connecting...

我尝试使用System.out调用来捕获它,但也没有任何反应。 然而,这绝对是好的:

@Override
public void init() {
    update();
}

哦,小程序当然也来自http://localhost:9000/

我该如何解决这个问题或修复它?

1 个答案:

答案 0 :(得分:2)

尝试以下方面的内容:

public void callFromJavaScript(final String param) {
    AccessController.doPrivileged( new PrivilegedAction<Void>() {
        public Void run() {
            // call code to make the connection..
            return null;
        }
    });
}