Display BLOB (image) through JSP

You’re making some fundamental mistakes here. The <img src> must point to an URL, not contain the image’s binary content. The content type of the JSP page itself should not be set to image/gif. It should be kept default to text/html. It is not true that the webserver is supposed to include the concrete images in the HTML result as you seemed to expect. It’s the webbrowser who downloads the images individually based on the URL found in src attribute and then presents them accordingly.

Easiest is to create a separate servlet which streams the image from the DB to the response body. You can uniquely identify the image by a request parameter or path info. Here’s an example which uses a request parameter for that:

<img src="https://stackoverflow.com/questions/11192020/imageServlet?id=<%=rsSuper.getString("id")%>" />

The doGet() method should then basically perform this job:

String id = request.getParameter("id");

// ...

InputStream input = resultSet.getBinaryStream("imageColumnName");
OutputStream output = response.getOutputStream();
response.setContentType("image/gif");
// Now write input to output the usual way.

Unrelated to the concrete problem, using scriptlets this way is officially strongly discouraged since a decade. Perhaps you were reading completely outdated books/tutorials or are maintaining an ancient JSP web application. For some insights, see also the answers of the following questions for some hints:

Leave a Comment