Tomcat 9 servlet: cannot decrease buffer size for writing the response

I found the strange behavior of the Tomcat 9 servlet: when a client is disconnected before the servlet returns the response, the response is still “returned” without raising any exception.

Here’s the test servlet:

public class TestClientDisconnectServlet extends HttpServlet {

    private static final Logger log = LoggerFactory.getLogger(TestClientDisconnectServlet.class);

    public TestClientDisconnectServlet() {
    }

    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        log.info("Start generating the response");
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("Finish generating the response");
        try {
            resp.setBufferSize(64);
            // Here we always get 8192, so when the client is disconnected, the error is not raised...
            // Even if I set in server.xml: <Connector ... socket.appWriteBufSize="1" /> it doesn't change anything!
            log.info("Buffer size=" + resp.getBufferSize());
            resp.setStatus(200);
            resp.setContentType("text/html");
            // If the line exceeds 8192 bytes, we get the exception
            String response = "<html><body>This is a test servlet.</body></html>";
            resp.getOutputStream().write(response.getBytes());
            resp.getOutputStream().flush();
            resp.getOutputStream().close();
            log.info("Succesfully delivered");
        } catch (Exception e) {
            log.info("Failed to deliver the request: {}", e.getMessage());
        }
    }
}

When a client is disconnected before getting the answer, the expected behavior is to raise an exception and output the message Failed to deliver the request. However the servlet is successfully executed up to Successfully delivered.

After some investigation, I found that the issue may be due to the write buffering: when a large (longer than 8192 bytes) request is returned, the servlet behaves as expected (raises an exception).

An obvious solution would be to decrease the write buffer size (as per the documentation, resp.setBufferSize() should do the trick). However it doesn’t work! When I pass the buffer size less than 8192, it remains equal to 8192.

I tried adding the attribute socket.appWriteBufSize to the Connector attribute in server.xml, as per the Tomcat documentation (I am using org.apache.coyote.http11.Http11NioProtocol, SSL-enabled). But it still cannot change the buffer size.

What am I doing wrong? How to reduce the write buffer size in HttpServletResponse, or, more general, how to determine when the client is disconnected during sending the (short) response?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật