tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From 流水音 <aoi...@gmail.com>
Subject How to do with response.sendError() after the ServletOutputStream is established?
Date Mon, 01 Jun 2009 05:55:14 GMT
HI,ALL:

Servlet code(deployed on Tomcat server):
    private void execute(HttpServletRequest req, HttpServletResponse
resp) throws ServletException, IOException {
           try {
                    int count = 0;
                    while(count< 1000000){
                         out.write(97); // writes 'a' to client
                         count++;
                         if(count == maxcount()){
                             throw new Exception("errors.");
                         }
                    }
            }catch(Exception e){
                 resp.sendError(605);
                 return;
            }
    }

    private int maxcount(){
         return 99999;// or return 15;
    }

Client code:

        URL url = new URL(urlAddress);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
	conn.setDoInput(true);
	conn.setUseCaches(false);
	conn.setAllowUserInteraction(false);
	conn.connect();
	InputStream input = null;
        try {
		input = conn.getInputStream();
		int count=0;
		while((input.read()) != -1){
			count++;
		}
		System.out.println("total read is: " + count);
	} catch (IOException e) {
                int code = conn.getResponseCode();
		System.out.println("Response code is: " + code);
	}finally{
           Closer.close(input);
        }

when the maxcount() returns 15, the exception is thrown out before the
first buffer is flushed, then client can get the response code of: 605
but when the maxcount() returns 99999, the exception is thrown out
after some buffers flushes, so the client can not get the exception
anymore, the response code is 200.

so, if I want to get the exception during the data transferring in the
client, do you guys have any idea hot to do that?

Thanks and Best Regards.

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


Mime
View raw message