code_junkie

Servlet long processing cancelation when browser closes

Nov 14th, 2011
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  2. {
  3. HttpSession session = request.getSession();
  4. System.out.println("Session " + session.getId() + " started");
  5.  
  6. response.setContentType("text/html;charset=UTF-8");
  7.  
  8. PrintWriter out = response.getWriter();
  9. try
  10. {
  11. while (!out.checkError())
  12. {
  13. try
  14. {
  15. Thread.sleep(1000);
  16. } catch (InterruptedException ex)
  17. {
  18. ex.printStackTrace();
  19. }
  20.  
  21. Date date = new Date();
  22.  
  23. // TODO append output to the client browser here
  24. out.println(".....");
  25.  
  26. System.out.println("Session " + session.getId() + "data sent at: " + date);
  27.  
  28. out.flush();
  29. //break; // _TEST
  30. }
  31. } finally
  32. {
  33. System.out.println("Session " + session.getId() + " finished");
  34. out.close();
  35. }
  36. }
Add Comment
Please, Sign In to add comment