坚持使用response.flushBuffer

时间:2016-06-23 13:47:38

标签: javascript java

有人可以找到此代码的错误。我试图将内容部分刷新到客户端。但它从初始值发送数据,而不是以块的形式发送数据。

public String execute()
{
    String action = request.getParameter("action");
    try 
    {
        switch(action)
        {
        case "flush":
        response.setContentType(ContentTypeConstants.APP_JSON_UTF8);
        PrintWriter pw = response.getWriter();
            for(int i = 0 ; i < 10; i++)
            {
               JSONObject json = new JSONObject();
               json.put("key", i+"");
               pw.print(json+"");
               response.flushBuffer();  
               Thread.sleep(500);
            }
            break;
        default:
            break;
        }
    }
    catch(Exception e)
    {   
        e.printStackTrace();
    }
    return null;
}

我的脚本代码是

var http=getHTTPObject();
http.open("GET", url, true);
http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
http.setRequestHeader("X-Requested-With","XMLHttpRequest");
http.onreadystatechange = function () {
    if (http.readyState == 3)
    {
        console.log(http.responseText)
    }
}
http.send();

0 个答案:

没有答案