aspose file tools*
The moose likes Servlets and the fly likes writing to response object after client closes connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "writing to response object after client closes connection" Watch "writing to response object after client closes connection" New topic
Author

writing to response object after client closes connection

Rade Koncar
Greenhorn

Joined: Sep 14, 2012
Posts: 9
Hello,

If servlet is doing some lengthy operation before it writes to response object, and client has the time to close the browser,
what happens when data is finally being written to response object?

Is this the same situation as when user clicks Stop button?

Can servlet somehow detect that user terminated the connection?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4639
    
    5

Rade Koncar wrote:If servlet is doing some lengthy operation before it writes to response object, and client has the time to close the browser,
what happens when data is finally being written to response object?


It goes into a black hole, never to be heard from again. Actually, I don't know where it goes, but it just goes away. Probably the allocated buffer is freed and garbaged collected. I never had any reason to investigate further.

Rade Koncar wrote:Is this the same situation as when user clicks Stop button?


No, not at all. If the user clicks a Stop button on a webpage, that usually just sends a new POST command with "stop"
This requires processing by whatever code is programmed to handle that command. So the exact details depend on what the programmer decided to engineer.

Rade Koncar wrote:Can servlet somehow detect that user terminated the connection?

Not quickly enough to do anything useful.

This is all fundamentally defined by the HTTP protocols. With HTTP, the client (browser) must initiate every action. The server is limited to responding to the request, once. The server (or servlet) can not initiate any actions outside the response to the client request. (It can, of course, if it wants, do anything in response to a client request, including talking to another thread, another server, etc.)
Rade Koncar
Greenhorn

Joined: Sep 14, 2012
Posts: 9
I tried writing this kind of servlet and deployed it in tomcat 6.0.36.
Indeed nothing happened. Then I checked if it generates some kind of exception that is silently caught by the container,
but nothing is being caught inside servlet.

I guess only way to detect it is to generate some kind of timeout exception when there is no communication on a TCP level for a long time...


Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4639
    
    5

Tomcat has included support for session timeouts after a period of inactivity. Its just a XML setting for the time, and a page/servlet to handle it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: writing to response object after client closes connection
 
Similar Threads
Multiple URLConnections
MVC Architecture.
MVC Architecture
Multiple response for a request
How to Close the Response Object at ServerSide?