Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting browser closed in middle of a http request response cycle

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 ?

like image 276
arunvg Avatar asked Oct 25 '25 04:10

arunvg


1 Answers

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

like image 109
lance-java Avatar answered Oct 26 '25 18:10

lance-java