response.setContentType("text/html"); PrintWriter out = response.getWriter(); // send some data to the client out.println(""); out.println("Partial content "); out.println(""); out.println("Partial content example
"); out.flush(); //force contents to be sent immediately // send more data to the client out.println("This is some more data
"); out.flush(); //force contents to be sent immediately // send the final data out.println("");
response.setContentType("application/octet-stream"); ServletOutputStream out = response.getOutputStream(); // send some binary data to the client byte[] data = new byte[] { 0x01, 0x02, 0x03, 0x04 }; out.write(data); out.flush(); //force contents to be sent immediatelyIn this example, a servlet sends some binary data to the client in a single `write` operation. After writing the data, the `flush` method is called to ensure the data is sent immediately instead of waiting for the entire response to be complete. The javax.servlet.http package library contains the HttpServletResponse class and related classes and interfaces for handling HTTP requests and responses in a servlet.