3

Scenario :

  1. Web application connecting to a java servlet. (Using embedded jetty)
  2. High latency request, example a report generation ( takes seconds)
  3. User can close the browser in between the http request response cycle.

Problem :

How to detect browser closed in between the request and response cycle, at the server side ?

Though the problem can be solved by using another request from the UI, I was wondering whether we could get the information from the communication protocol itself ? As HTTP is using TCP/IP as the base protocol, shouldn't we be able to detect this as a connection closed from the connection initiator side.

I went through the servlet apis and also the api used for configuring jetty server but couldn't find anything closely related.

Also went through these stackoverflow questions

  1. Java Servlet : How to detect browser closing?
  2. How to detect browser close at server side in asp.net?

Any insights ?

Community
  • 1
  • 1
arunvg
  • 1,209
  • 1
  • 20
  • 31

1 Answers1

2

You'll get an exception when you attempt to write to the servlet output stream / writer.

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • 1
    Thank you for the response. Basically you are pointing to the out.checkError() method. I was looking for an event based mechanism, that I can detect **in between** and possibly stop processing. – arunvg Oct 12 '15 at 08:56
  • I don't think such an event exists but you could fire your own event by listeneing to `window.close()` in the client (javascript) and fire off a second ajax request. Eg: `$(window).close(new function() { ... })` – lance-java Oct 12 '15 at 10:45
  • the server has no event to trigger from in this situation. only an attempt to use the connection will cause the event to be noticed. this can be a read from the request inputstream, or a write to the response outputstream. – Joakim Erdfelt Oct 12 '15 at 14:30
  • 1
    Thank you for the response Joakim. Slowly getting closer to the fact that from java it may not be possible to get the notification of a connection closed from a TCP initiator. I was checking from bottom up, the TCP protocol specifies a handshake (between the initiator and receiver) in this situation. I am currently checking the feasibility of it in a java environment and trying to understand why it is not provided in regular java libraries. – arunvg Oct 13 '15 at 04:10
  • 1
    This post http://stackoverflow.com/questions/155243/why-is-it-impossible-without-attempting-i-o-to-detect-that-tcp-socket-was-grac do a good job in why it is no possible to detect a connection closed, without communicating. Hard realities :) – arunvg Oct 13 '15 at 05:00
  • 1
    @arunvg and don't forget, Java (and HTTP/0.9, and HTTP/1.0, and HTTP/1.1, and HTTP/2, and Servlets, and WebSocket) support half-closed connections. – Joakim Erdfelt Oct 13 '15 at 13:07
  • @JoakimErdfelt thank you. Can you extend the hint a bit :) . Not able to find anything related to half close connections and servlets. If a client closed event can be detect in between, the design for a resource optimized service provisioning would have much leaner and effective, than correlating it through another request – arunvg Oct 14 '15 at 04:54