在doGet方法完成后,Servlet是否会返回响应? [英] Does Servlet return response after doGet method has finished?

查看:203
本文介绍了在doGet方法完成后,Servlet是否会返回响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然, doGet 方法的返回类型为void,因此,它不会返回任何内容。在这个意义上,我使用返回这个词来表示将响应发送回请求它的客户端。

Obviously, the doGet method has a return type of void, so, it doesn't return anything. In this sense, I'm using the word "return" to mean send the response back to the client that requested it.

我正在尝试实现一个长期的轮询Servlet。如果我有想要寄回的东西,就不要发送回复是有益的。因此,在doGet方法中,我将连接用户的ID和AsyncContext添加到地图:

I'm trying to implement a long-polling Servlet. It would be beneficial for it not to send a response until I have something that I would like to send back. So, in the doGet method I add the connected user's ID and AsyncContext to a map:

private ConcurrentMap<String, AsyncContext> contexts = new ConcurrentHashMap<>();
//...in the doGet method when I need to add the context...
contexts.put(userId, context);

然后,当我有东西发回时,我可以检索适当的上下文并写入它的回复输出流:

Then, when I have something to send back, I can retrieve the appropriate context and write to it's responses output stream:

AsyncContext context = contexts.get(userId);
PrintWriter writer = context.getResponse().getWriter();
writer.write("something to send to the client");

但是,客户似乎永远不会收到回应。查看浏览器开发人员控制台中的网络选项卡,我可以看到GET请求已发送,然后返回(状态为200)。这发生在我实际发送回来之前。这让我相信在doGet方法完成后会返回响应。也许正因为如此,在此之后,没有任何东西可以发送到客户端,因为没有打开连接。

But, the client never seems to receive the response. Looking at the Network tab in the developer console of the browser, I can see the GET request is sent and then returns (with a status of 200). This occurs before I actually send something back. Which is leading me to believe that after the doGet method is finished the response is returned. And perhaps because of this, after this point, nothing can be sent to the client because the connection is not opened.

一旦方法执行完毕,doGet方法是否会将响应发送给客户端?如果是这种情况,如何保持连接打开以获得长轮询效果?

Does the doGet method send the response to the client once the method is finished executing? If this is the case, how can I keep the connection open for a long-polling effect?

推荐答案

回答我自己的问题:一旦方法执行完毕,doGet方法是否会将响应发送到客户端?

是,当doGet(或任何HttpServlet方法,例如:doGet,doPost等)方法完成执行后将响应发送回客户端。

Yes, when the doGet (or any HttpServlet method, ex: doGet, doPost, etc.) method finishes executing it sends the response back to the client.

如果在这种情况下,如何保持连接打开以获得长轮询效果?

使用异步Servlet(我正在使用,但是,我发现我的特定问题必须在其他地方,但这些答案仍然与所提出的问题相关)。在ServletRequest对象上调用startAsync方法,如下所示:

Using asynchronous Servlets (which I was using, however, I found my particular problem must be elsewhere, yet these answers are still relevant to the questions asked). On the ServletRequest object call the startAsync method, like so:

AsyncContext context = request.startAsync(request, response);

这将通知Web容器在请求调用结束时它应该释放处理线程并保持连接打开,以便其他线程写入响应并结束连接。参考链接。

"This will notify the Web Container that at the end of the request call it should free the handling thread and leave the connection open so that other thread writes the response and end the connection."Reference Link.

此外,我将为我的特定问题添加解决方案(客户端没有收到响应)是因为在我的Servlet中,我没有在AsyncContext对象上调用complete方法:

Also, I will add the solution to my particular problem (the client wasn't receiving the response) was because in my Servlet, I wasn't calling the complete method on the AsyncContext object:

asyncContext.complete();

这篇关于在doGet方法完成后,Servlet是否会返回响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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