Saturday, October 25, 2025
HomeLanguagesJavaServlet – Input Stream Class

Servlet – Input Stream Class

ServletInputStream class is a component of Java package javax.servlet, It is an abstract class that 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.

Syntax:

public abstract class ServletInputStream extends InputStream

Constructor 

ServletInputStream() : Since ServletInputStream is an abstract class therefore it cannot be initialized.

Note : ServletRequest.getInputStream() method is used to get the reference of ServletInputStream.

Methods 

ServletInputStream contains only one concrete method named as readLine.

readLine(byte [ ] b, int offset, int len):

  1. It is a part of ServletInputStream class.
  2. It is used to read the input stream.
  3. It will return a number of bytes read or -1.
  4. It might throw IOException if an input or output exception occurs.

Method Signature:

public int readLine(byte[] b, int offset,int len) throws IOException.

Method Parameters: readLine() method has three parameters which are byte, int, and int type.

Method Return Type: readLine() method has an int return type and will return a number of bytes read or -1 if the end of the stream is reached.

Abstract Methods of ServletInputStream

S.No.  

Method                                     

Description                                                                                    

Return Type                        

1. isFinished() isFinished() method will return true if all the data from the stream has been read otherwise it will return false. abstract boolean
2. isReady()     isReady() method will return true if the data from the stream can be read without blocking otherwise it will return false. abstract boolean
3. setReadListener(ReadListener readListener)

setReadListener method is used to instruct the ServletInputStream 

to invoke the provided ReadListener when it is possible to read.

abstract void

Interfaces Implemented by ServletInputStream

  • java.io.Closeable .
  • java.lang.AutoCloseable .

Java Program to Create a Servlet and to Check if Data of the Stream can be Read:

Java




import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GeeksForGeeks extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
         
    {
        try {
            ServletInputStream servletInputStream
                = request.getInpuStream();
            System.out.println(
                "Data of stream can be read : "
                + servletInputStream.isReady());
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
    {
        doGet();
    }
}


Output:

true

Note: The above code will not run on online IDE since this is server-side code.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS