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="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:
- How to avoid Java code in JSP files?
- Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
- How to retrieve and display images from a database in a JSP page?
How to display multiple MySQL db blob images in a jsp page table using JSTL
One approach is to create a servlet that will return the image with the specified id and then just use a regular <img src="/imgserv?${row.imageid}"/>
(or along those lines).
Another recent possibility is to use embedded, base64 encoded image data as described in Embedding Base64 Images. That will require you to preprocess the byte data to Base64 though, but it would avoid having to create a separate servlet for fetching the image data (not that there's anything wrong with it). Of course if you have many large images, the added overhead of the embedded base64 can become an issue.
Related Topics
Stream Filter of 1 List Based on Another List
How to Use Bufferedreader in Java
Apache Httpclient 4.0.3 - How to Set Cookie with Sessionid for Post Request
How Can a Keylistener Detect Key Combinations (E.G., Alt + 1 + 1)
How to Stop the Task Scheduled in Java.Util.Timer Class
Passing an Array or List to @Pathvariable - Spring/Java
Java Swing Jtextfield Set Placeholder
Java Unreachable Catch Block Compiler Error
How to Insert a Row Between Two Rows in an Existing Excel with Hssf (Apache Poi)
Dynamic Generic Typing in Java
Any Simple Way to Explain Why I Cannot Do List<Animal> Animals = New Arraylist<Dog>()
How to Determine the Ip of My Router/Gateway in Java
Pros and Cons of Package Private Classes in Java
Java: How to Use Urlconnection to Post Request with Authorization