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:
This file creates a link that invokes the servlet. The url-pattern of the servlet is servlet1.
This servlet class reads the image from the mentioned directory and writes the content in the response object using ServletOutputStream and BufferedOutputStream classes.
download this example (developed using Myeclipse IDE)
download this example (developed using Eclipse IDE)
download this example (developed using Netbeans IDE)