wood burning stoves*
The moose likes Servlets and the fly likes How to detect  http client  is cancelled / killed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to detect  http client  is cancelled / killed?" Watch "How to detect  http client  is cancelled / killed?" New topic
Author

How to detect http client is cancelled / killed?

Howard Zhao
Greenhorn

Joined: May 06, 2002
Posts: 27
I am coding servlet part in my project. Now I am facing a problem on how to detect the http client that sends http query to my servlet is cancelled / killed by users.

I have a working scenario like this: a user sends http query to my servlet, my servlet parses query data and according to the contents of the http query, my servlet issues SQL calls via JDBC. When SQL queries finish, the servlet reformats the SQL results and send them back to http client. For some reason, either the SQL takes really long time or the DB server is slow, or whatever, during the execution period of SQL queries, my servlet just waits there and sends nothing back to http client. From the front user's standpoint (the front user issues http queries), he thinks that his http query is "dead", and he performs a kill or cancel operation to stop the http query.

Now my servlet needs to detect this kind of action, while it is still waiting for SQL queries to end, in order to cancel the SQL queries and to save DB resources.

How to do this?

Howard

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Originally posted by Howard Zhao:
Ihe thinks that his http query is "dead", and he performs a kill or cancel operation to stop the http query.

How does the user perform this kill or cancel operation? Closes the browser? Navigates to another page? Unplugs the computer?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Howard Zhao
Greenhorn

Joined: May 06, 2002
Posts: 27
Thank you for your prompt reply. I am really appreciated.

The user can:
1 close the browser if he is using browser to send http query to the servlet.
2 perform a �Cancel� operation in a MFC based c++ client side program. The mentioned program constructs http query and sends it to the servlet.
3 close the mentioned program.

Howard
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Detecting browser close is difficult to do, so it's best to just let that one go. With regards to fat clients, you can do whatever you want using the facilities of the programming platform. But there's nothing built into the Servlet API to help you out with this. You'll need to come up with your own notification sub-system.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
You can't detect the browser closing, but you will get an IOException when your servlet tries to write to the response output stream and finds it has been closed by the client. Perhaps your servlet could write and flush a few bytes every N seconds - this would require that you run the database query in a separate Thread.

Bill
[ December 20, 2007: Message edited by: William Brogden ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to detect http client is cancelled / killed?
 
Similar Threads
The Perennial Lazy loading dilemma from the heart of a novice
jdbc cancel query
Converting sql queries while porting
Cancel database query....
how to fetch data from resultset containing result of two queries