You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Fang Yidong <fa...@yahoo.com.cn> on 2003/05/23 11:33:05 UTC

Re: How can I get notified when the HTTP client close the connection?

Tim,I'm so confused as you.
In fact,I'm developing a download management system,one component of which is the servlet like this:
 
public class MyServlet extends HttpServlet{
 
public doGet(...){
   OutputStream out=response.getOutputStream();
   
   while(<conditions>){
       out.write(<data>);
   }
   ...
   finally{
       out.close();
   }
}
}//class MyServlet
 
Since the data may be large,the client may abort the data transportation during the "while" cycles,then an IOException would be expected when the servlet retry out.write(<data>).But the truth is that there isn't any exception raised when out.write(<data>).The data seems to be discarded silently and the servlet can not see anything abnormal,so the servlet thread won't exit until it finished to "write" all of the data the output stream "out",which now points to a null device.
 
It seems that this problem is relevant to the implementation of the web container.Should the developers of Tomcat or the editor of the servlet specification be aware of it?
 
 
Tim wrote:
>After a quick glance at the spec, I don't see any place which states 
>the
>server must throw an exception on client abort. There is a reference in
>ServletOutputStream throwing IOExceptions but it is very vague.
>Try the same code in a servlet and see what happens. (I don't know if 
>it will
>be helpful or not)
>-Tim
>Fang Yidong wrote:
>> The problem is: I can't catch any Exception at all.
>>  
>> Tim wote:
>>  



---------------------------------
Do You Yahoo!?
"相见不如聊天!不出门一样面对面!网络摄像头对对派送中~赶快用你的雅虎电邮帐号参与吧……

Re: How can I get notified when the HTTP client close the connection?

Posted by Tim Funk <fu...@joedog.org>.
I was able to catch the browser stop just fine. Here is the servlet I used,
start with this as a basis:

package more;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Cowbell extends HttpServlet {
    protected void doGet(HttpServletRequest request,
                          HttpServletResponse response)
                   throws ServletException, IOException {
        response.setContentType("text/plain");
        response.setBufferSize(10);
        try {
            ServletOutputStream out = response.getOutputStream();
            int i = 0;
            while (true) {
                out.println(i++ + "");
                out.flush();
                Thread.sleep(100);
                if (i>100000) { /* 166 minutes is long enough */
                    return;
                }
            }
        } catch(IOException e) {
            log("IOException", e);
            throw e;
        } catch(Throwable e) {
            log("Throwable", e);
            throw new ServletException(e);
        }
    }
}

in web.xml
  <servlet>
    <servlet-name>prescription</servlet-name>
    <servlet-class>more.Cowbell</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>prescription</servlet-name>
    <url-pattern>/fever</url-pattern>
  </servlet-mapping>


So I have a url called "/fever" which maps to the servlet "prescription", and
the prescription is more.Cowbell.

Have fun!

-Tim

Fang Yidong wrote:
> Tim,I'm so confused as you.
> In fact,I'm developing a download management system,one component of which is the servlet like this:
>  


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org