小编典典

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

java

我有一个带有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/

我该如何解决或解决它?


阅读 181

收藏
2020-11-26

共1个答案

小编典典

尝试以下方法:

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