Servlet Input Output Stream Classes

Servlet Input Output Stream Classes

In this article, I am going to discuss Servlet Input Output Stream Classes with Examples. Please read our previous article where we discussed Servlet Annotations with Examples. At the end of this article, you will understand the following pointers in detail.

  1. Servlet Input Stream Class
  2. Constructors of ServletInputStream
  3. Methods of ServletInputStream
  4. Servlet Output Stream Class
  5. Constructors of ServletOutputStream
  6. Methods of ServletOutputStream
  7. Servlet Input Output Stream Classes Example
Servlet Input Stream Class

It provides an input stream for reading binary data from a client request, including an efficient readLine() method for reading data one line at a time. A ServletInputStream object can be used to read data sent from the client with some protocols, such as HTTP POST and PUT. It is normally retrieved via ServletRequest.getInputstream() method. A servlet container implements this abstract class. The java.io.InputStream.read() method is implemented by the subclasses of this class.

Constructors of ServletInputStream

protected ServletInputStream(): It does nothing because this is an abstract class.

Methods of ServletInputStream

public int readLine(byte[] b, int off, int len): It is used to read an input stream, one line at a time. Starting at an offset, it reads bytes into an array, until it reads a certain number of bytes or reaches a newline character, which it reads into the array as well. The method usually returns an integer specifying the actual number of bytes read, or -1 if the end of the stream is reached. It throws an IOException if an input or output exception has occurred.

Here,
       b: Is an array of bytes into which data is read.
       off: is an integer specifying the character at which this method begins reading.
       len: It is an integer specifying the maximum number of bytes to read.

Servlet Output Stream Class

ServletOutput stream provides an output stream for sending binary data to the client. A ServletOutputStream object is normally retrieved via the ServletResponse, getOutputStream() method. A servlet container implements this abstract class. The java.io.OutputStream.write(int) method is implemented by the subclasses of this class.

Constructors of ServletOutputStream

protected ServletOutputStream(): It does nothing because it is an abstract class.

Methods of ServletOutputStream
  1. public void print (String s): It is used to write a String to the client, without a carriage return line feed(CRLF) character at the end. It throws an IOException if an input or output exception occurred. Here, the parameter “s” is the String to send to the client.
  2. public void print(boolean b): It is used to write a boolean value to the client, without a carriage return line feed(CRLF) character at the end. It throws an IOException if an input or output exception occurred. Here, the parameter “b” is the Boolean value to send to the client.
  3. public void print(char c): It is used to write int to the client, without a carriage return line feed(CRLF) character at the end. It throws an IOException if an input or output exception occurred. Here, the parameter “c” is the character value to send to the client.
  4. public void print(int i): It is used to write a String to the client, without a carriage return line feed(CRLF) character at the end. It throws an IOException if an input or output exception occurred. Here, the parameter “i” is the int to send to the client.
  5. public void print(long l): It is used to write a long value to the client, without a carriage return line feed(CRLF) character at the end. It throws an IOException if an input or output exception occurred. Here, the parameter “l” is the long value to send to the client.
  6. public void print(float f): It is used to write a float value to the client, without a carriage return line feed(CRLF) character at the end. It throws an IOException if an input or output exception occurred. Here, the parameter “f” is the float value to send to the client.
  7. public void print(double d): It is used to write a double value to the client, without a carriage return line feed(CRLF) character at the end. It throws an IOException if an input or output exception occurred. Here, the parameter “d” is the double value to send to the client.
  8. public void println(): It is used to write a carriage return line feed(CRLF) to the client. It throws an IOException if an input or output exception occurred.
  9. public void println(String s): It is used to write a String to the client, followed by a carriage return line feed(CRLF). It throws an IOException if an input or output exception occurred. Here, the parameter “s” is the String to write to the client.
  10. public void println(boolean b): It is used to write a Boolean value to the client, followed by a carriage return line feed(CRLF). It throws an IOException if an input or output exception occurred. Here, the parameter “b” is the Boolean value to write to the client.
  11. public void println(char c): It is used to write a character to the client, followed by a carriage return line feed(CRLF). It throws an IOException if an input or output exception occurred. Here, the parameter “c” is the character to write to the client.
  12. public void println(int i): It is used to write an int to the client, followed by a carriage return line feed(CRLF). It throws an IOException if an input or output exception occurred. Here, the parameter “i” is the int to write to the client.
  13. public void println(long l): It is used to write a long value to the client, followed by a carriage return line feed(CRLF). It throws an IOException if an input or output exception occurred. Here, the parameter “l” is the long value to write to the client.
  14. public void println(float f): It is used to write a float value to the client, followed by a carriage return line feed(CRLF). It throws an IOException if an input or output exception occurred. Here, the parameter “f” is the float value to write to the client.
  15. public void println(double d): It is used to write a double value to the client, followed by a carriage return line feed(CRLF). It throws an IOException if an input or output exception occurred. Here, the parameter “d” is the double value to write to the client.
Servlet Input Output Stream Classes Example

In his example, we are using ServletOutputStream class for writing the image and the FileInputStream class to read the image content. We have used the BufferedInputStream and BufferedOutputstream class to make the performance faster. Here we have to use the content type image/jpeg. We have a rose.jpeg image inside the G:\JAVA directory. You can change the location accordingly.

Here, the index.html file creates a link that invokes the servlet. The URL-pattern of the servlet is “servlet1”. ServletIOExample.java servlet class reads the image from the mentioned directory and writes the content in the response object using ServletOutputStream and BufferedOutputStream classes. Here, web.xml deployment descriptor is the configuration file of web application which instructs the servlet container which classes to load and hot to intercept requests coming from browsers.

index.html
<html>
    <head>
        <title>Display Image</title>
    </head>  
    <body>    
        <a href="servlet1">click for photo</a>  
    </body>
</html>
ServletIOExample.java
import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  
public class ServletIOExample extends HttpServlet {  
    public void doGet(HttpServletRequest request,HttpServletResponse response)  
             throws IOException  
    {  
    response.setContentType("image/jpeg");  
    ServletOutputStream out;  
    out = response.getOutputStream();  
    FileInputStream fin = new FileInputStream("G:/JAVA/rose.jpg");  
      
    BufferedInputStream bin = new BufferedInputStream(fin);  
    BufferedOutputStream bout = new BufferedOutputStream(out);  
    int ch =0; ;  
    while((ch=bin.read())!=-1)  
    {  
    bout.write(ch);  
    }  
      
    bin.close();  
    fin.close();  
    bout.close();  
    out.close();  
    }  
} 
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>ServletIOExample</servlet-name>
    <servlet-class>ServletIOExample</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ServletIOExample</servlet-name>
    <url-pattern>/servlet1</url-pattern>
  </servlet-mapping>
</web-app>
Output

Run your index.html file and you will get the following output.

Servlet Input and Output Stream classes with Examples

Click on the link to get the output

Servlet Input and Output Stream class with Example

In the next article, I am going to discuss the SingleThreadModel interface in Servlet. Here, in this article, I try to explain Servlet Input and Output Stream classes with Examples. I hope you enjoy this Servlet Input and Output Stream class article.

Leave a Reply

Your email address will not be published. Required fields are marked *