Open In App

Servlet – Output Stream Class

Last Updated : 13 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
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. void
2. print(char c) print(char c) method is used to write a character to the client with no CRLF character at the end. void
3. print(double d) print(double d) method is used to write a double value to the client with no CRLF character at the end. void
4. print(float f) print(float f) method is used to write a float value to the client with no CRLF character at the end. void
5. print(int i) print(int i) method is used to write an int value to the client with no CRLF character at the end. void
6. print(long l) print(long l) method is used to write a long value to the client with no CRLF character at the end. void
7. print(String s) print(String s) method is used to write a String to the client with no CRLF character at the end. void
8. println() println() method is used to write a CRLF character at the end. void
9. println(boolean b) println(boolean b) method is used to write a boolean value to the client, followed by a CRLF at the end. void
10. println(char c) println(char c) method is used to write a character to the client, followed by a CRLF at the end. void
11. println(double d) println(double d) method is used to write a double value to the client, followed by a CRLF at the end. void
12. println(float f) println(float f) method is used to write a float value to the client, followed by a CRLF at the end. void
13. println(int i) println(int i) method is used to write an int value to the client, followed by a CRLF at the end. void
14. println(long l) println(long l) method is used to write a long value to the client, followed by a CRLF at the end. void
15. println(String s) println(String s) method is used to write a String to the client, followed by a CRLF at the end. void

Abstract Methods of ServletOutputStream

S.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 void
2. isReady() isReady() method is used to determine if data can be written without blocking. abstract boolean

Interfaces Implemented by ServletOutputStream 

  1. java.io.Closeable.
  2. java.io.Flushable.
  3. 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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads