JSP - JS - Java interaction

JSP - JS - Java interaction

本文关键字:interaction Java JS JSP      更新时间:2023-09-26

我试图从JS调用Java applet中的方法。我对如何安排我的工作流程有点困惑。我有一个jsp (pending.jsp)类,其中包括一个javascript (pending.js)。jsp只是一个容器来保存pending.js。Pending.js的目的是让用户能够取消创建报表。

pending.jsp:

<div id="pending-reports-container" class="pending-reports-container">
    <div id="pending-reports">
    </div>
</div>

pending.js:

function update_pending_reports(response_data)
{ bla }
function delete_report(report_id)
{ bla }
function request_pending_reports()
{ bla }
function start_pending_reports() {
    request_pending_reports();
    setInterval("request_pending_reports()", 5000);
}

因为取消报告有时不是那么有效,我想使用Java来取消到postgres的请求(pending.js不会杀死在postgres工作的进程)。我创建了我的Java类,但我不知道在哪里添加它。我知道我可以使用标记,但是当我尝试像这样将这个标记添加到我的jsp文件时:

<div id="pending-reports-container" class="pending-reports-container">
    <div id="pending-reports">
       <applet id="LS" width=1 height=1 code ="module/LicenseCheckService.class" codebase="."/>
    </div>
</div>

我无法从我的pending.js中使用以下代码访问它:

 getJS = document.getElementById("LS");

是我的代码有问题,还是我在这个结构上有问题?

参见从JavaScript代码Oracle文档中调用Applet方法。

简而言之就是JavaScript通过applet ID来引用applet对象,并且可以调用它的方法。

appletId.anAppletMethod();
var anAppletClass = appletId.getAnAppletClass();
anAppletClass.methodCalled("with a JavaScript string parameter");
// Etc.

看Dave Newton的回答。

或者,您也可以检查HTML字段或从Applet调用javascript方法。您可以构造由applet主动控制的交互。

链接到这里:Java Applet To JS Link