【问题标题】:Passing data from java file to ajax将数据从java文件传递到ajax
【发布时间】:2018-07-01 19:11:39
【问题描述】:

我有一个 java 类将进程的进度打印为 1%、2%、3%。现在我想使用 AJAX 请求读取这个进度并不断更新网页中的进度。我无法将 java 文件转换为 servlet,因为它被 Jasper 用作脚本。

Scriptlet 代码是:

public void afterDetailEval() throws JRScriptletException{
     count = (Integer) this.getVariableValue("count"); 
     out.println(count);
     Thread.sleep(200); 
}

如何从 AJAX 请求中读取 java 打印的数据?

任何帮助将不胜感激!

【问题讨论】:

  • 您如何看待您选择不显示正在执行您刚才描述的事情的代码,同时还就这些事情寻求帮助?
  • 你的java类在里面运行什么? Tomcat 还是类似的?
  • @williamburnham 是的,它在 tomcat 中运行
  • @kumasena 现在完成

标签: javascript java ajax jsp


【解决方案1】:

您的 scriptlet 是如何创建的?唯一想到的是如果你有一个像

public class YourScriptlet extends JRDefaultScriptlet {
    public void afterDetailEval() throws JRScriptletException {
        // your code here
    }
}

您可以为另一个对象添加一个构造函数和私有成员,该对象充当您在 Jasper 上下文之外所需的任何内容的容器,就像这样

一些对象来跟踪计数(或任何你需要跟踪的对象)

public class YourInfoObject {
    private final AtomicInteger count = new AtomicInteger();
    public int increment() {
        return this.count.incrementAndGet();
    }
    public int get() {
        return this.count.intValue();
    }
    public void set(int value) {
        this.count.set(value);
    }
}

您的带有构造函数的 scriptlet 类

public class YourScriptlet extends JRDefaultScriptlet {
    private final YourInfoObject obj;
    public YourScriptlet(YourInfoObject obj) {
        this.obj = obj;
    }
    public void afterDetailEval() throws JRScriptletException {
        // your code here
        obj.set(count);
    }
}

然后,您可以从其他任何地方(引用您的对象的 Servlet)访问该值。

【讨论】:

  • 非常感谢您的回答!
猜你喜欢
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
  • 2013-12-21
相关资源
最近更新 更多