- Java EE Tutorials
- Servlets Tutorials
- Servlets Interview Questions
javax.servlet.ServletInputStream and javax.servlet.ServletOutputStream are abstract classes. These classes define several key methods that the other stream classes implement. Two of the most important methods are read( ) and write( ), which are used for reading the client request and writing servlet response to client respectively.
ServletInputStream
The ServletInputStream class extends java.io.InputStream. It is implemented by the servlet container and provides an input stream, that a developer can use to read the data from a client request.
Methods of ServletInputStream
- readline( ) method is used to read the input values.
ServletOutputStream
ServletOutputStream is an abstract class in the javax.servlet package. It is used in servlets to write the response to the client. ServletOutputStream class extends java.io.OutputStream. It also defines the print( ) and println( ) methods, which output data to the stream.
Methods of ServletOutputStream
Methods Name | Description |
print(boolean value) | Prints boolean value(true/false). |
print(string) | Prints string values. |
print(char) | Prints a single character . |
print(float) | Prints float value( uses 32 bits). |
print(double) | Prints double value(uses 64 bits). |
print(int) | This prints an integer type value. |
print(long) | It prints long value i.e. large value(64 bit). |
println(boolean value) | Prints boolean value(true/false). |
println(string) | Prints string values. |
println(char) | Prints a single character. |
println(float) | Prints float value( uses 32 bits). |
println(double) | Prints double value(uses 64 bits) . |
println(int) | This prints an integer type value. |
println(long) | It prints long value i.e. large value(64 bit). |
Example
Now let use see an example which demonstrates the use of these classes. This class tries to open the image lily.JPG.
package javabeat.net.servlets; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.IOException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletIOExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("image/jpeg"); ServletOutputStream out; out = response.getOutputStream(); FileInputStream img = new FileInputStream("/home/manisha/Pictures/lily.JPG"); BufferedInputStream bin = new BufferedInputStream(img); BufferedOutputStream bout = new BufferedOutputStream(out); int ch = 0; ; while ((ch = bin.read()) != -1) { bout.write(ch); } bin.close(); img.close(); bout.close(); out.close(); } }
Web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>ServletIOExample</servlet-name> <servlet-class>javabeat.net.ServletIOExample</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletIOExample</servlet-name> <url-pattern>/ServletIOExample</url-pattern> </servlet-mapping> </web-app>
Output:
Previous Tutorial : Difference Between Forward And sendRedirect || Next Tutorial : How To Initialize Variables In Servlet?