• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

ServletInputStream and ServletOutputStream

February 6, 2014 //  by Krishna Srinivasan//  Leave a Comment

  • 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:
servlet_io_demo

Previous Tutorial : Difference Between Forward And sendRedirect  || Next Tutorial : How To Initialize Variables In Servlet?

Category: Java EETag: Servlets Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Difference Between Forward And sendRedirect In Servlet
Next Post: How To Initialize Variables In Servlet »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact