Note that there are some explanatory texts on larger screens.

plurals
  1. POServlet long processing cancelation when browser closes
    text
    copied!<p>I have a servlet which processes a request for a long time. It suppose to keep doing stuff in the loop inside doPost and send data through response's out writer. Effectively that continuously appends data in the clients browser . But the problems accures when client just closes the browser. Inspite of the broken connection the response's writer stream in the servlet never gets closed, thus servlet is unaware of the brocen connection, and keep dumping data into the writer without any errors. How is that posssible? And how do I detect and cancel long request processing in case of browser disconnect?</p> <p>I thougth checkError() on the response writer should do the trick, but it does not seam to work. Any ideas why? </p> <p>This is the servlet code which never stops:</p> <pre><code>protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); System.out.println("Session " + session.getId() + " started"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { while (!out.checkError()) { try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } Date date = new Date(); // TODO append output to the client browser here out.println("....."); System.out.println("Session " + session.getId() + "data sent at: " + date); out.flush(); //break; // _TEST } } finally { System.out.println("Session " + session.getId() + " finished"); out.close(); } } </code></pre> <p>Thanks,<br> Mike</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload