Home » Example to Display Image using Servlet

Example to Display Image using Servlet

by Online Tutorials Library

Example to display image using Servlet

In this example, we are using FileInputStream class to read image and ServletOutputStream class for writing this image content as a response. To make the performance faster, we have used BufferedInputStream and BufferedOutputStream class.

You need to use the content type image/jpeg.

In this example, we are assuming that you have java.jpg image inside the c:test directory. You may change the location accordingly.

To create this application, we have created three files:

  1. index.html
  2. DisplayImage.java
  3. web.xml

index.html

This file creates a link that invokes the servlet. The url-pattern of the servlet is servlet1.


DisplayImage.java

This servlet class reads the image from the mentioned directory and writes the content in the response object using ServletOutputStream and BufferedOutputStream classes.

Next TopicServlet Quiz

You may also like