AJAX(原型/ JAVA)得到执行过程中的部分状态更新 [英] AJAX (prototype/java) getting partial status updates during execution

查看:87
本文介绍了AJAX(原型/ JAVA)得到执行过程中的部分状态更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这部分模仿<一href="http://stackoverflow.com/questions/800997/ajax-prototype-php-getting-partial-status-updates-during-script-execution">AJAX (原型/ PHP)在脚本执行得到部分状态更新,但我正在用JSP页面和servlet。我想要做的是,当用户点击一个按钮,然后在$这一行动的进展情况p $ psent更新启动操作。该动作可以在任何地方,从1到10分钟就可以完成,所以我不希望用户只是坐在屏幕上等待响应,而是显示一个状态栏或某事表示什么行动的一部分,该交易对

This partially mimics AJAX (prototype/php) getting partial status updates during script execution, however I'm working with JSP pages and servlets. What I want to do is start an action when the user clicks a button and then present updates on the progress of this action. The action can take anywhere from 1 to 10 minutes to complete, so I don't want the user just sitting on the screen waiting for a response but rather display a status bar or something denoting what part of the action the transaction is on.

感谢您

推荐答案

如果你想运行和控制长期运行的进程,更好地让它在自己的发执行主题> 。存储的引用,这个在session范围内螺纹,以便客户端可以使用ajaxical请求(使用同一个会话!),要求对目前的进度在服务器端(和自动的还保持会话活着,使其不超时)。

If you want to run and control a long-running process, better let it run in its own Thread instead of the request's Thread. Store a reference to this Thread in the session scope so that the client can use ajaxical requests (using the same session!) to request the server side for the current progress (and automagically also to keep the session alive so that it doesn't timeout).

下面是这样一个servlet的一个基本的例子:

Here's a basic example of such a servlet:

package mypackage;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RunLongProcessServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        if ("XMLHttpRequest".equals(request.getHeader("x-requested-with"))) {
            LongProcess longProcess = (LongProcess) request.getSession().getAttribute("longProcess");
            response.setContentType("application/json");
            response.getWriter().write(String.valueOf(longProcess.getProgress()));
        } else {
            request.getRequestDispatcher("runLongProcess.jsp").forward(request, response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        LongProcess longProcess = new LongProcess();
        longProcess.setDaemon(true);
        longProcess.start();
        request.getSession().setAttribute("longProcess", longProcess);
        request.getRequestDispatcher("runLongProcess.jsp").forward(request, response);
    }

}

class LongProcess extends Thread {

    private int progress;

    public void run() {
        while (progress < 100) {
            try { sleep(1000); } catch (InterruptedException ignore) {}
            progress++;
        }
    }

    public int getProgress() {
        return progress;
    }

}

..这是映射如下:

..which is mapped as follows:

<servlet>
    <servlet-name>runLongProcess</servlet-name>
    <servlet-class>mypackage.RunLongProcessServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>runLongProcess</servlet-name>
    <url-pattern>/runLongProcess</url-pattern>
</servlet-mapping>

和这里的JSP的一个基本的例子(用小镜头 jQuery的,一个ajaxical JS框架,我的方式大大推荐):

And here's a basic example of the JSP (with a little shot jQuery, an ajaxical JS framework which I by the way greatly recommend):

<!doctype html>
<html lang="en">
    <head>
        <title>Show progress of long running process with help of Thread and Ajax.</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(init);

            function init() {
                if (${not empty longProcess}) {
                    $.progress = 0;
                    checkProgress();
                }
            }

            function checkProgress() {
                $.getJSON('runLongProcess', function(progress) {
                    $('#progress').text(progress);
                    $.progress = parseInt(progress);
                });
                if ($.progress < 100) {
                    setTimeout(checkProgress, 1000);
                }
            }
        </script>
    </head>
    <body>
        <form action="runLongProcess" method="post">
            <p>Run long process: <input type="submit"></p>
            <p>Current status: <span id="progress">0</span>%</p>
        </form>
    </body>
</html>

HTTP打开它://本地主机:8080 / yourcontext / runLongProcess 并点击按钮

如果这是真的,真的长期运行的过程中,你可能会提高效率通过增加Ajax请求的时间间隔的setTimeout() 5秒(5000毫秒)左右,以使服务器不感到得到DDOS'ed)

If this is a really, really long running process, you may improve "efficiency" by increasing the ajax request intervals in the setTimeout() to 5 seconds (5000 ms) or so, so that the server doesn't feel getting DDOS'ed ;)

希望这有助于。

这篇关于AJAX(原型/ JAVA)得到执行过程中的部分状态更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆