Servlet - Output Stream Class Last Updated : 13 Jan, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report ServletOutputStream class is a component of Java package javax.servlet, is an abstract class that provides an output stream to send binary data to the client. ServletOutputStream inherits OutputStream which is the superclass of all classes representing an output stream of bytes. Subclasses of ServletOutputStream class must implement the java.io.OutputStream.write(int) method. Signature public abstract class ServletOutputStream extends OutputStream Constructor ServletOutputStream() : Since ServletOutputStream is an abstract class therefore it cannot be initialized. Note: ServletResponse.getOutputStream() method is used to get the reference of ServletOutputStream. Methods S.No Method Description Return Type 1.print(boolean b) print(boolean b) method is used to write a boolean value to the client with no CRLF character at the end.void2.print(char c)print(char c) method is used to write a character to the client with no CRLF character at the end.void3.print(double d)print(double d) method is used to write a double value to the client with no CRLF character at the end.void4.print(float f)print(float f) method is used to write a float value to the client with no CRLF character at the end.void5.print(int i)print(int i) method is used to write an int value to the client with no CRLF character at the end.void6.print(long l)print(long l) method is used to write a long value to the client with no CRLF character at the end.void7.print(String s)print(String s) method is used to write a String to the client with no CRLF character at the end.void8.println()println() method is used to write a CRLF character at the end.void9.println(boolean b)println(boolean b) method is used to write a boolean value to the client, followed by a CRLF at the end.void10.println(char c)println(char c) method is used to write a character to the client, followed by a CRLF at the end.void11.println(double d)println(double d) method is used to write a double value to the client, followed by a CRLF at the end.void12.println(float f)println(float f) method is used to write a float value to the client, followed by a CRLF at the end.void13.println(int i)println(int i) method is used to write an int value to the client, followed by a CRLF at the end.void14.println(long l)println(long l) method is used to write a long value to the client, followed by a CRLF at the end.void15.println(String s)println(String s) method is used to write a String to the client, followed by a CRLF at the end.voidAbstract Methods of ServletOutputStreamS.No Method Description Return Type 1.setWriteListener(WriteListener writeListener) setWriteListener(WriteListener writeListener) method is used to instructs the ServletOutputStream to invoke the provided WriteListener when it is possible to write. abstract void2.isReady()isReady() method is used to determine if data can be written without blocking.abstract booleanInterfaces Implemented by ServletOutputStream java.io.Closeable.java.io.Flushable.java.lang.AutoCloseable. Java program to create a servlet and then send an image as a response - Java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class GeeksForGeeks extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { try { // set response content type response.setContentType("image/jpg"); // get the reference of servletOutputStream ServletOutputStream servletOutputStream; servletOutputStream = response.getOutputStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream( new FileInputStream( "c:/gfg/geeks.jpg")); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream( servletOutputStream); // read from buffer and // then write that data to // bufferedOutputStream int tmp; while (1) { tmp = bufferedInputStream.read(); if (tmp == -1) break; bufferedOutputStream.write(tmp); } } catch (Exception e) { System.out.println(e.getMessage()); } } public void doPost(HttpServletRequest request, HttpServletResponse response) { doGet(); } } Note: The above code will not run on online IDE since this is server-side code. Comment More infoAdvertise with us Next Article Servlet - Output Stream Class H harshsethi2000 Follow Improve Article Tags : Java java-servlet Practice Tags : Java Similar Reads HttpServlet Class In Java HttpServelt is an abstract class, it comes under package 'javax.servlet.http.HttpServlet' . To create a servlet the class must extend the HttpServlet class and override at least one of its methods (doGet, doPost, doDelete, doPut). The HttpServlet class extends the GenericServlet class and implements 6 min read Java.io.OutputStreamWriter Class In Java, OutputStreamWriter class connects character streams to byte streams. It encodes Characters into bytes using a specified charset. Declaration of Java OutputStreamWriter Class public class OutputStreamWriter extends WriterConstructors of OutputStreamWriter Class in JavaConstructors in OutputS 5 min read Java.io.OutputStream class in Java This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink. Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. Constructo 2 min read Generic Servlet Class GenericServlet implements the Servlet interface and provides an implementation for all its method except the service() method hence it is abstract. GenericServlet class defines a protocol-independent(HTTP-less) servlet. However, while building a website or an online application, we may want to have 3 min read Steps to Create a Servlet Today we all are aware of the need of creating dynamic web pages i.e the ones which have the capability to change the site contents according to the time or are able to generate the contents according to the request received by the client. If you like coding in Java, then you will be happy to know t 2 min read Like