Servlet InputOutputStream

As of now, we have done with servlet Page Redirection also we have seen interfaces and classes in servlet. In this article, we will discuss ServletInputStream and ServletOutputStream. As we know, ServletInputStream and ServletOutputStream is a class for reading and writing binary data. Let us see in detail.

What is Stream?

Java has I/O streams to read data and write data.

  • A stream is an input source or an output source destination which could be a file, I/O device, or any program.
  • InputStream is used to read data from the source and OutputStream is used to write data to the source.

What is ServletInputStream?

ServletInputStream is a class that provides stream to read binary data from a request. With the ServletInputStream object, we can use it to read data sent from the client. It is an abstract class. The ServletInputStream object can be retrieved by

ServletRequest.getInputStream()

Method of ServletInputStream

There is one method in this class.

  • int readLine(byte[] b, int off, int len): This method is used to read the input line by line. This method returns an integer which specifies the actual number of bytes read.
    • b: an array of bytes from which the data will be read.
    • off: It is an offset that specifies from which the data is being read.
    • len: It is an integer that specifies the length of bytes to read.

What is ServletOutputStream?

ServletOutputStream is a class that provides a stream to write binary data into the response. With the ServletOutputStream object, we can use it to write data sent to the client. It is an abstract class. The ServletOutputStream object can be retrieved by

ServletResponse.getOutputStream()

Methods of ServletOutputStream

We have mostly used println() of the following methods in the previous examples.

  • void print(boolean b): This method writes the boolean value to the client.
  • void print(char c): This method writes the character to the client.
  • void print(double d): This method writes double to the client.
  • void print(float f): This method writes float value to the client.
  • void print(int i): This method writes integer value to the client.
  • void print(long l): This method writes a long value to the client.
  • void print(String s): This method writes string value to the client.
  • void prinln(): This method writes the CRLF(Carriage Return-Line feed)
  • void println(boolean b): This method writes the boolean value to the client.
  • void println(char c): This method writes the character to the client followed by CRLF.
  • void println(double d): This method writes double to the client followed by CRLF.
  • void println(float f): This method writes float value to the client followed by CRLF.
  • void println(int i): This method writes integer value to the client followed by CRLF.
  • void println(long l): This method writes a long value to the client followed by CRLF.
  • void println(String s): This method writes string value to the client followed by CRLF.

Example to Display Image using ServletOutputStream

In this example, we are using ServletOutputStream class to write image content as a response.

index.html

<body>
<h1><a href="IOStream">Click here</a></h1>
</body>

Web.xml

<servlet>
    <servlet-name>IOStream</servlet-name>
    <servlet-class>IOStream</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>IOStream</servlet-name>
    <url-pattern>/IOStream</url-pattern>
</servlet-mapping>

IOStream.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("image/png");
    ServletOutputStream outputStream = response.getOutputStream();
    FileInputStream fileInputStream = new FileInputStream("E:\\java\\java.png");
    BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
    int ch = 0;
    while ((ch = bufferedInputStream.read()) != -1) {
      bufferedOutputStream.write(ch);
    }

  }

Output