Saturday, October 11, 2025
HomeLanguagesJavaServlet – Display Image

Servlet – Display Image

The FileInputStream class is used to read the picture, and the ServletOutputStream class is used to write the image content as a response. We used the BufferedInputStream and BufferedOutputStream classes to improve performance. We must utilize the image/jpeg content type. Within the C:\Images directory, there is a GFG.jpeg image. You can adjust the location as needed. We’re going to make the following pages in this example:

Example

We created three files to make this application:

  • index.html
  • DisplayImage.java
  • web.xml

The index.html file creates a servlet-invoking link. The servlet’s URL pattern is “servlet1.” The ServletIOExample.java servlet class reads the picture from the specified directory and uses the ServletOutputStream and BufferedOutputStream classes to write the content to the response object.

1. index.html

HTML




<html>
    <head>
        <title>Display GFG Image</title>
    </head>  
    <body>
         <a href="DisplayImageGfg">click for photo</a>  
    </body>
</html>


2. ServletIOExample.java

Java




import java.io.*;
import javax.servlet.*;  
import javax.servlet.http.*;  
  
public class DisplayImageGfg extends HttpServlet 
{  
    public void doGet(HttpServletRequest request,HttpServletResponse response)  
             throws IOException  
    {  
        // set the content type to image/jpeg.
        response.setContentType("image/jpeg");  
          
        ServletOutputStream out;
          
        // Writing this image 
        // content as a response 
        out = response.getOutputStream(); 
          
        // path of the image
        FileInputStream fin = new FileInputStream("C:\\Images\\GFG.jpeg");  
  
        // getting image in BufferedInputStream  
        BufferedInputStream bin = new BufferedInputStream(fin);
        BufferedOutputStream bout = new BufferedOutputStream(out);  
          
        int ch =0;  
        while((ch=bin.read())!=-1)  
        {  
            // display image
            bout.write(ch);  
        }  
          
        // close all classes
        bin.close();  
        fin.close();  
        bout.close();  
        out.close();  
    }  
}


3. web.xml

XML




<web-app>
  
  <servlet>
    <servlet-name>DisplayImageGfg</servlet-name>
    <servlet-class>DisplayImageGfg</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>DisplayImageGfg</servlet-name>
    <url-pattern>/DisplayImageGfg</url-pattern>
  </servlet-mapping>
    
</web-app>


Output:

When you run your index.html file, you’ll see the following results.

Output

To get the output, click on the link.

Output

RELATED ARTICLES

Most Popular

Dominic
32352 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11884 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7103 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS