PC.
PC.

Reputation: 7024

Java's HttpServletResponse doesn't have isClientConnected method

I'm implementing a long poll http connection using java servlet.

How can I know that the http client is still active at any instance? Currently, what I do is to write a byte to the output stream and flush data. If there's an IO exception then the client is dead.

But in ASP.NET there is a property, Response.IsClientConnected which can find out if the client is active without writing anything to the output stream.

I want to know how if it is possible to develop in java servlet. I do not want to keep writing data into the http response stream as it may cost network.

Thanks in advance.

Upvotes: 8

Views: 1856

Answers (2)

Santosh
Santosh

Reputation: 17903

It will be difficult to achieve that using Servlet APIs. Though the low level Socket APIs provide this functionality (Socket.isConnected() ), but same functionality is not available through any higher level APIs. Not sure if you any compulsions of using Servlet APIs or you can use low level socket APIs.

Upvotes: 3

Alex Abdugafarov
Alex Abdugafarov

Reputation: 6412

Maybe you've taken the wrong approach? HTTP protocol is developed to be used in a request-response style, it is not suited to be used for a long polling. In fact, there should be lowest possible delay before client gets a server response.

The case you've described looks like a job for a good old Socket.

Upvotes: -1

Related Questions