简体   繁体   English

第二次调用HttpServletResponse

[英]Call HttpServletResponse a second time

I'm trying to set up a servlet that I can use to call webservices asynchronously. 我正在尝试设置一个可用于异步调用Web服务的servlet。 For the most part, it is working fine. 在大多数情况下,它运行良好。 I have a servlet with a doGet method and a js that calls it. 我有一个带有doGet方法的servlet和一个调用它的js。 I have a callback method in the js that the servlet correctly calls when it has finished doing its thing. 我在js中有一个回调方法,该servlet在完成工作后会正确调用。

The complication is that one of the web services I am calling is also asynchronous, and I would like to be able to essentially call the js callback method a second time after the asynchronous ws callback has finished. 复杂之处在于,我正在调用的Web服务之一也是异步的,并且我希望能够在异步ws回调完成后第二次实质上调用js回调方法。 For example, if you have a status field, when you call the synchronous web service, it immediately updates to "Beginning Synchronous Call" and then when the servlet callback arrives it changes to the callback value, which is the result of the web service. 例如,如果您有一个状态字段,则在调用同步Web服务时,它将立即更新为“ Beginning Synchronous Call”,然后在servlet回调到达时,它将更改为回调值,这是Web服务的结果。

When you call the asynchronous web service, the update field immediately updates to "Beginning Asynchronous Call", and shortly receives the first callback from the servlet indicating that the web service has been requested, so we update the field to "Processing Web Service" or whatever. 当您调用异步Web服务时,更新字段立即更新为“开始异步调用”,并很快从Servlet接收到表明已请求Web服务的第一个回调,因此我们将该字段更新为“正在处理Web服务”或随你。 The problem is that once the web service finishes and calls back to the servlet, I can't seem to figure out how to send the result to the js callback method. 问题在于,一旦Web服务完成并回调了servlet,我似乎就无法弄清楚如何将结果发送到js回调方法。

I'm pretty new at AJAX and servlets, so maybe this is a horrible way to accomplish what I want. 我在AJAX和servlet方面还很陌生,所以也许这是完成我想要的事情的可怕方法。

The web services are both being called in the Servlet, mostly using Netbeans auto-generated WS calls. Web服务都在Servlet中被调用,大多数情况下使用Netbeans自动生成的WS调用。 The WS calls themselves work fine, but once I get the result of the asynchronous WS, I am stuck inside of the handleResponse method of the webservice callback and no longer have any reference to the response element for the document I want to update. WS可以正常工作,但是一旦获得异步WS的结果,我就会陷入webservice回调的handleResponse方法中,并且不再引用要更新的文档的response元素。

I tried to store the original response variable as a static member variable and use it in the handleResponse method like so: 我试图将原始响应变量存储为静态成员变量,并在handleResponse方法中使用它,如下所示:

javax.xml.ws.AsyncHandler<WsClients.Op11Response> asyncHandler = new javax.xml.ws.AsyncHandler<WsClients.Op11Response>() {

    public void handleResponse(javax.xml.ws.Response<WsClients.Op11Response> asyncResponse) {
        try {
            storedResponse.setContentType("text/xml");
            String returnString = asyncResponse.get().getReturn();
            storedResponse.getWriter().write("<returnData><content>"
                    + returnString + "</content></returnData>");

        } catch (Exception ex) { 
        }
    }
};

This will not compile with a debugger attached and does not seem to be able to assign a reference anyway. 这将无法与附加的调试器一起编译,而且似乎也无法分配引用。

Is there a better way to do this? 有一个更好的方法吗?

The nature of HTTP is that you cannot send anything back to the client unless client requested this information either by polling or by keeping the connection open. HTTP的本质是,除非客户端通过轮询或通过保持连接打开来请求此信息,否则您无法将任何内容发送回客户端。

The operation to start the asynchronous call ends immediately and you need to return from the servlet doGet method (while technically you can stay in the servlet call until your async call finishes I wouldn't recommend that as it ties up the server resources. It is generally a good practice to return from the servlet as soon as you can). 启动异步调用的操作立即结束,您需要从servlet doGet方法返回(从技术上讲,您可以一直停留在servlet调用中,直到异步调用完成为止,我不建议这样做,因为它占用了服务器资源。通常,最好是尽快从servlet返回)。

The best course of action would be: 最好的措施是:

  1. Have internal data structure (eg HashMap with appropriate synchronization) to hold the asynchronous calls that are executing. 具有内部数据结构(例如具有适当同步的HashMap)来保存正在执行的异步调用。
  2. When you start a new call, assign it pseudo-random key and return it from the initial call. 当您开始一个新的呼叫时,为其分配伪随机密钥,并从初始呼叫中返回它。
  3. Using the above key, have browser-side javascript AJAX calls periodically poll the status of the call and display the results. 使用上面的键,让浏览器端的javascript AJAX调用定期轮询调用的状态并显示结果。
  4. Do not forget to clean up finished or stale calls (for example by running a timer thread). 不要忘记清理完成的或过时的调用(例如,通过运行计时器线程)。

When you comfortable with the polling implementation in step 3 above, you may want to consider Comet, aka long poll to replace client-side polling. 当您满意上面第3步中的轮询实现时,您可能需要考虑Comet,又名长轮询,以取代客户端轮询。

Servlet cannot send response again. Servlet无法再次发送响应。 HTTP protocol is synchronous, and only client can initiate a request-response exchange. HTTP协议是同步的,只有客户端可以发起请求-响应交换。

For async updates you need to perform polling from the client side to the server side, and accumulate messages on the server side (in the sessions) until client picks them up or they expire. 对于异步更新,您需要执行从客户端到服务器端的轮询,并在服务器端(在会话中)累积消息,直到客户端将其接收或过期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpServletResponse 的 sendRedirect 调用无法执行 - HttpServletResponse's sendRedirect call could not execute Facebook身份验证第二次未调用onActivityResult - Facebook authentication not call onActivityResult from second time 无法再次致电身份验证提供程序 - can not call auth provider for second time hit 递归调用方法,但仅第二次登录? - recursively call a method but log only second time? 应该在 HttpServletResponse.getOutputStream()/.getWriter() 上调用 .close() 吗? - Should one call .close() on HttpServletResponse.getOutputStream()/.getWriter()? 第二种方法调用所需的时间远少于第一种方法 - Second method call takes far less time than the first 在java中,如果你第二次调用相同的函数,程序会“记住”结果吗? - In java, will the program “remember” the result if you call the same function for the second time? 对AlarmManager.setRepeating()的第二次调用在错误的时间传递 - Second call to AlarmManager.setRepeating() gets delivered at wrong time 第一次填充HashSet无效-仅在第二次调用时 - Filling a HashSet doesn't work the first time — only on second call 初始化 HttpServletResponse - Initializing HttpServletResponse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM