13

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?

3 Answers 3

12

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.

Sign up to request clarification or add additional context in comments.

9 Comments

"Nothing you have to do about that." - and nothing you CAN do about it.
I edited the question to clarify. I'd like not to have to send spurious data to the client unless I absolutely have to.
You won't. You will get an exception instead, and the container will deal with it.
No because there isn't a way in TCP/IP to detect it without writing any data. - what about RST packet?
The option of "just complete the request" is in many cases not good. For example, in the system I am supporting, there could be some user queries made but they take very long time to complete without writing any data. The user could just disconnected however the request is still being processed until the end, which in some cases these kinds of requests occupy all the Tomcat connections. Is it a way to avoid this?
|
5

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).

There the finally block is for. It will be executed regardless of the outcome. E.g.

OutputStream output = null; try { output = response.getOutputStream(); // ... output.flush(); // ... } finally { // Do your cleanup here. } 

If I have the HttpServletRequest available, will trying to read from that throw an IOException if the client disconnects?

Depends on how you're reading from it and how much of request body is already in server memory. In case of normal form encoded requests, whenever you call getParameter() beforehand, it will usually be fully parsed and stored in server memory. Calling the getInputStream() won't be useful at all. Better do it on the response instead.

2 Comments

Request body isn't in the server memory, until you read it (by using request input stream, or calling getParameter() in case of POST with application/x-www-form-urlencoded). Request body can be of any size, server can't just cache it in memory.
@Peter: I've eliminated the ambiguity.
3

Have you tried to flush the buffer of the response: response.flushBuffer(); Seems to throw an IOException when the client disconnected.

1 Comment

It will not throw unless it really has something to send to the client.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.