检测tomcat servlet中的客户端断开连接? [英] Detecting client disconnect in tomcat servlet?

查看:152
本文介绍了检测tomcat servlet中的客户端断开连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测tomcat servlet请求的客户端是否已断开连接?我已经读过我应该做一个response.getOutputStream()。print(),然后是一个response.getOutputStream()。flush()并捕获一个IOException,但有没有一种方法可以检测到它而不写任何数据? / p>

编辑



servlet发出的数据流不一定结束,但不一定有任何数据流过它(这是一个实时事件流)。我需要实际检测客户端何时断开连接,因为我需要进行一些清理工作(要释放的资源等)。如果我有可用的HttpServletRequest,如果客户端断开连接,会尝试读取抛出IOException吗?

解决方案


有没有办法可以在不写任何数据的情况下检测到这个


不,因为没有在TCP / IP中检测它而不写任何数据的方法。



不要担心。只需完成请求操作并编写响应即可。如果客户端已经消失,那将导致IOException:connection reset,它将被抛入servlet容器中。你无需做任何事情。


How can I detect that the client side of a tomcat servlet request has disconnected? I've read that I should do a response.getOutputStream().print(), then a response.getOutputStream().flush() and catch an IOException, but is there a way I can detect this without writing any data?

EDIT:

The servlet sends out a data stream that doesn't necessarily end, but doesn't necessarily have any data flowing through it (it's a stream of real time events). I need to actually detect when the client disconnects because I have some cleanup I have to do at that point (resources to release, etcetera). If I have the HttpServletRequest available, will trying to read from that throw an IOException if the client disconnects?

解决方案

is there a way I can detect this without writing any data?

No because there isn't a way in TCP/IP to detect it without writing any data.

Don't worry about it. Just complete the request actions and write the response. If the client has disappeared, that will cause an IOException: connection reset, which will be thrown into the servlet container. Nothing you have to do about that.

这篇关于检测tomcat servlet中的客户端断开连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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