Skip to content
Advertisement

how to clear contents of ServletOutputStream?

I’m wondering how to delete all contents from ServletOutputStream (or any other OutputStream) in case when exception is occurred at some point and I need outputStream to be empty so that it doesn’t include “half of data”

doSmthInServlet(HttpServletResponse respone, ....) {
        try {
            ServletOutPutStream outputStream = response.getOutputStream();
            //.....
            //writing to outputstream
            //exception occurred
        } catch (Exception e) {
            // and here I need to clear all the contents of outputStream
        }
    }

Advertisement

Answer

ServletResponse.resetBuffer() to clear the buffer.

ServletResponse.reset() to clear the buffer, headers and response code.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement