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

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

问题描述

如何检测 tomcat servlet 请求的客户端已断开连接?我读过我应该做一个 response.getOutputStream().print(),然后一个 response.getOutputStream().flush() 并捕获一个 IOException,但是有没有一种方法可以在不写入任何数据的情况下检测到这一点?

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?

编辑:

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

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?

不,因为 TCP/IP 没有办法在不写入任何数据的情况下检测它.

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

别担心.只需完成请求操作并编写响应即可.如果客户端消失了,将导致 IOException: connection reset ,这将被抛出到 servlet 容器中.对此您无需采取任何措施.

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天全站免登陆