代码之家  ›  专栏  ›  技术社区  ›  kowsikbabu

将数据从java文件传递到ajax

  •  -1
  • kowsikbabu  · 技术社区  · 8 年前

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

    scriptlet代码是:

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

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

    任何帮助都将不胜感激!

    1 回复  |  直到 8 年前
        1
  •  1
  •   geco17    8 年前

    如何创建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)可以访问该值。