5

This is how it goes : A web based application running on Tomcat 6. A client sends a request to the server, server does some processing, makes a redirect to 3rd Party URL and again does some processing.

Before doing redirect, I want to know if the client connection is still open or not (so that I can prevent the second processing). I know sendRedirect throws IOException & IllegalStateException and those can be handled in case redirect fails.

I have also tried to obtain the output stream from response and write any junk characters to the stream. But to no use (even if I remove the client machine from the LAN, after invoking the request)

I want to know - is there any way I can identify if the connection between Client and Server is still active?

kaps
  • 81
  • 2
  • 9
  • You can check the following posts: http://stackoverflow.com/questions/1390024/how-do-i-check-if-a-socket-is-currently-connected-in-java http://stackoverflow.com/questions/698964/checking-if-a-clientsocket-has-disconnected-in-java-hangs – Adel Boutros Dec 30 '11 at 11:02
  • what's wrong with trying the redirect and not doing the second processing if an IOException is thrown? – JB Nizet Dec 30 '11 at 11:16

1 Answers1

2

Old question but I was dealing with a similar problem and found a solution.

Aborted HTTP connection can indeed be detected only if the server sends something back to the client and that fails. To force sending the data to the client, you must call flushBuffer() on the HttpServletResponse. If the connection is aborted, this throws ClientAbortException.

andysh
  • 339
  • 1
  • 15