简体   繁体   English

Java检查HttpResponse是否仍然有效

[英]Java Check if HttpResponse is Still Alive

Is there a way from a java servlet to check if the httpresponse is still "alive?" java servlet有没有一种方法可以检查httpresponse是否仍然“有效”? For instance, in my situation I send an ajax request from the browser over to a servlet. 例如,在我的情况下,我将ajax请求从浏览器发送到servlet。 In this case its a polling request so it may poll for up to 5 minutes, when the servlet is ready to respond with data i'd like to check if the user has closed the browser window, or moved to another page etc. In other words, check to see if sending the data to the response will actually do anything. 在这种情况下,它是一个轮询请求,因此当servlet准备好用数据响应时,它最多可能轮询5分钟,我想检查用户是否已关闭浏览器窗口或移至另一个页面等。的话,请检查将数据发送到响应是否实际上会执行任何操作。

Java Servlet Response doesn't have any such method as it is based on request and response behavior. Java Servlet Response没有任何此类方法,因为它基于请求和响应行为。 If you wish to check the status, then probably you need to work at lower level eg TCP/IP Sockets , which has several status check methods as below: 如果要检查状态,则可能需要在较低级别上工作,例如TCP / IP套接字 ,它具有以下几种状态检查方法:

boolean isBound()
Returns the binding state of the socket.

boolean isClosed()
Returns the closed state of the socket.

boolean isConnected()
Returns the connection state of the socket.

boolean isInputShutdown()
Returns whether the read-half of the socket connection is closed.

boolean isOutputShutdown()
Returns whether the write-half of the socket connection is closed.

Generally, this problem can be solved by sending a dummy payload before the actual message. 通常,可以通过在实际消息之前发送虚拟有效负载来解决此问题。

If the socket was severed, an IOException or a SocketException or something similar is thrown (depending on the library). 如果套接字被切断,则IOExceptionSocketException或类似的东西(取决于库)。 Technically, browsers are supposed to sever a connection whenever you navigate away from a page or close the browser (or anything similar), but I've found out that the implementation details can vary. 从技术上讲,浏览器应该在您离开页面或关闭浏览器(或类似的东西)时切断连接,但是我发现实现细节可能会有所不同。 Older versions of FF, for example, appropriately close a connection when navigating away from a page, but newer versions (especially when using AJAX) tend to leave connections open. 例如,较旧版本的FF在导航离开页面时会适当关闭连接,但是较新版本(尤其是在使用AJAX的情况下)往往会使连接保持打开状态。

That's the main reason you may use a dummy packet before the actual message. 这就是您可能在实际消息之前使用虚拟数据包的主要原因。 Another important consideration is the timeout. 另一个重要的考虑因素是超时。 I've done polling before and you either need to implement some sort of heartbeat to keep a connection alive or increase the server timeout (although keep in mind that some browsers may have timeouts as well - timeouts that you have no control over). 我之前已经做过轮询,您可能需要实施某种脉动信号以保持连接存活或增加服务器超时(尽管请记住,某些浏览器也可能会发生超时-您无法控制的超时)。

Instead of polling or pushing over AJAX, I strongly suggest trying to support (at least in part) a Websocket solution. 我强烈建议不要(至少部分地)支持Websocket解决方案,而不是轮询或推送AJAX。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM