Java HttpServletResponse has no isClientConnected method

I am using a long poll HTTP connection using a Java servlet.

How can I find out that the http client is still active anyway? I am currently doing this to write a byte to the output stream and dump the data. If there is an IO exception, then the client is dead.

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

I want to know how you can develop in a Java servlet. I do not want to write data to the HTTP response stream, as it can cost the network.

Thanks in advance.

+7
source share
2 answers

This will be difficult to achieve using the Servlet APIs. Although the low-level Socket APIs provide this functionality ( Socket.isConnected() ), the same functionality is not available through any higher-level APIs. Not sure if you are using Servlet APIs, or you can use low-level socket APIs.

+3
source

Maybe you were wrong? The HTTP protocol is designed for use in the style of a request-response, it is not suitable for long polling. In fact, there should be the smallest possible delay before the client receives a server response.

The case you described looks like work for an old old socket.

-one
source

All Articles