本文介绍了如何从servlet流文本响应到jsp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行很长时间的servlet进程,正在从JSP调用.

我想将servlet进程的状态发送给客户端.

除了流媒体部分之外,我都能按照我想要的方式工作.

我正在使用ajax调用servlet,并在响应中填充文本区域.但是,响应会立即全部发送到客户端,而不是一步一步地流化.

我希望客户端在发生每个响应(flush())时看到它们,而不是在通话结束时立即看到所有响应.

Ajax呼叫:

  postUploadFile = function(){$(#status").val(");YES.getAndShowStatus("status","$ {home}/Upload");}YES.getAndShowStatus = function(listenerId,url,successFunction){jQuery.get(URL,函数(数据){val = jQuery(#" + listenerId).val();jQuery(#" + listenerId).val(val + data);});}; 

Servlet代码:

  @Override受保护的void doPost(HttpServletRequest req,HttpServletResponse resp)抛出ServletException,IOException {log.info("----------");log.info(正在发布");resp.setContentType("text/plain");OutputStream out = resp.getOutputStream();out.write("FOOBAR 1 \ n" .getBytes());out.flush();out.write("FOOBAR 2 \ n" .getBytes());out.flush();out.write("FOOBAR 3 \ n" .getBytes());out.flush();log.info(完成".);} 

我最终得到了这个,但是并没有看到进展.我需要怎么做才能看到进度?

解决方案

此问题中发布的原始代码有一个大问题和几个小问题.最大的问题是以下行: resp.setContentType("text/plain"); .将此行更改为 resp.setContentType("application/text"); 可以解决服务器端的流问题.对客户端代码进行了更多实质性的更改.下面的代码有效,并且是一个完整的工作解决方案,用于将多部分表单发布到服务器并在发生时从服务器获取流响应(几个值经过硬编码,需要清除,但可以完成工作)./p>

服务器代码:

  @Override受保护的void doPost(HttpServletRequest req,HttpServletResponse resp)抛出ServletException,IOException {log.info("----------");log.info(正在发布");resp.setContentType(应用程序/文本");OutputStream out = resp.getOutputStream();out.write(将文件写入服务器" .getBytes());out.flush();writeZipFileToDisc(req,resp);out.write("\ n \ nDone.\ n" .getBytes());out.flush();log.info(完成".);} 

客户代码:

  postUploadFile = function(){$(#status").val(");YES.getAndShowStatus("status","$ {home}/Upload");}YES.getAndShowStatus = function(listenerId,url,successFunction){//基于https://gist.github.com/sohelrana820/63f029d3aa12936afbc50eb785c496c0var form = $(#uploadForm")[0];var data = new FormData(form);$ .ajax({类型:"POST",编码类型:"multipart/form-data",网址:url,数据:数据,processData:否,contentType:false,快取:false,xhrFields:{//获得进度流式响应进行中:功能(e){var progressResponse;var response = e.currentTarget.response;val = jQuery(#" + listenerId).val();jQuery(#" + listenerId).val(response);}}});}; 

I have a long running servlet process that is being called from a JSP.

I would like to send the status of the servlet process to the client as it is happening.

I have everything working the way I want except for the streaming part.

I'm using ajax to call the servlet and populating a text area with the response. The response, however, is sent to the client all at once at the end instead of streamed step by step.

I'd like the client to see each of the responses (flush()) as they happen, rather than all at once at the end of the call.

Ajax calls:

postUploadFile = function() {
    $("#status").val("");
    YES.getAndShowStatus("status", "${home}/Upload");
}


YES.getAndShowStatus = function(listenerId, url, successFunction) {
    jQuery.get(url,
        function(data) {
            val = jQuery("#" + listenerId).val();
            jQuery("#" + listenerId).val(val + data);
        }
    );
};

Servlet code:

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.info("----------");
        log.info("Doing post");
        resp.setContentType("text/plain");
        OutputStream out = resp.getOutputStream();
        out.write("FOOBAR 1\n".getBytes());
        out.flush();
        out.write("FOOBAR 2\n".getBytes());
        out.flush();
        out.write("FOOBAR 3\n".getBytes());
        out.flush();
        log.info("Done.");
    }

I get this in the end but do not see the progress as it occurs. What do I need to do to see the progress as it occurs?

解决方案

There was one big issue and a few small issues with the original code posted in this question. The big issue was this line: resp.setContentType("text/plain");. Changing this line to resp.setContentType("application/text"); fixed the streaming issue on the server side. More substantial changes were made to the client code. The code below works and is a complete working solution for posting a multipart form to a server and getting a streaming response from the server as it occurs (several values are hard coded and need to be cleaned up but it gets the job done).

Sever Code:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    log.info("----------");
    log.info("Doing post");
    resp.setContentType("application/text");
    OutputStream out = resp.getOutputStream();
    out.write("Writing file to server".getBytes());
    out.flush();
    writeZipFileToDisc(req, resp);
    out.write("\n\nDone.\n".getBytes());
    out.flush();
    log.info("Done.");
}

Client Code:

postUploadFile = function() {
    $("#status").val("");
    YES.getAndShowStatus("status", "${home}/Upload");
}

YES.getAndShowStatus = function(listenerId, url, successFunction) {
    // based on https://gist.github.com/sohelrana820/63f029d3aa12936afbc50eb785c496c0
    var form = $("#uploadForm")[0];
    var data = new FormData(form);
    $.ajax({
        type: "POST",
        enctype: 'multipart/form-data',
        url: url,
        data: data,
        processData: false,
        contentType: false,
        cache: false,
        xhrFields: {
            // Getting on progress streaming response
            onprogress: function(e)
            {
                var progressResponse;
                var response = e.currentTarget.response;
                val = jQuery("#" + listenerId).val();
                jQuery("#" + listenerId).val(response);

            }
        }
    });

};

这篇关于如何从servlet流文本响应到jsp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:28
查看更多