How to retrieve and display images from a database in a JSP page?

Let's see in steps what should happen: To display an image in HTML, you need the HTML element. To let it locate an image, you need to specify its src attribute. The src attribute needs to point to a valid URL and specify the image as request pathinfo (e.g. /images/foo.

Gif) or as request parameter (e.g. /images? Id=1). In JSP/Servlet world, you can let a Servlet listen on a certain url-pattern so that you can just execute some Java code on specific URL's.

Images are binary data and are to be obtained as an InputStream from the DB, the JDBC API offers the ResultSet#getBinaryStream() for this.In the Servlet you can just write this InputStream to the OutputStream of the response the usual Java IO way. The client side needs to be instructed that the data should be handled as an image, thus at least the Content-Type response headers needs to be set as well. That should be it.It almost writes code itself.

Let's start with HTML (in JSP): Oh, you can if necessary also dynamically generate links with EL: Then define/create a servlet which listens on /images/: imageServlet com.example. ImageServlet imageServlet /images/* "Plain vanilla" requests (such as image links) are obviously GET, thus just do the thing in doGet(): // content=blob, contentType=varchar(255), contentLength=integer, name=varchar(255) UNIQUE. Private static final String SQL_FIND = "SELECT content, contentType, contentLength FROM Image WHERE name =?

"; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Prepare. String name = request.getPathInfo(). Substring(1); // Returns "foo.

Gif". You may want to do nullchecks here to avoid NPE's. Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; try { // Query DB.

Connection = database.getConnection(); statement = connection. PrepareStatement(SQL_FIND); statement. SetString(1, name); resultSet = statement.executeQuery(); if (resultSet.next()) { // Image found, prepare response and send it.

Response. SetContentType(resultSet. GetString("contentType")); response.

SetContentLength(resultSet. GetInt("contentLength")); response. SetHeader("Content-Disposition", "inline;filename=\"" + name + "\""); BufferedInputStream input = null; BufferedOutputStream output = null; try { input = new BufferedInputStream(resultSet.

GetBinaryStream("content")); output = new BufferedOutputStream(response.getOutputStream()); byte buffer = new byte1024; for (int length; (length = input. Read(buffer)) > -1;) { output. Write(buffer, 0, length); } } finally { if (output!

= null) try { output.close(); } catch (IOException logOrIgnore) {} if (input! = null) try { input.close(); } catch (IOException logOrIgnore) {} } } else { // No image found, send HTTP 404.Response. SendError(HttpServletResponse.

SC_NOT_FOUND); } } catch (SQLException e) { throw new ServletException("Something failed at SQL/DB level. ", e); } finally { if (resultSet! = null) try { resultSet.close(); } catch (SQLException logOrIgnore) {} if (statement!

= null) try { statement.close(); } catch (SQLException logOrIgnore) {} if (connection! = null) try { connection.close(); } catch (SQLException logOrIgnore) {} } } That's it. Another example and more hints can be found in this article.

1 Yet another great answer from a great person. Thanks! – asgs Jun 14 at 18:08 +1 Excellent Answer.. .

– Bilal Jul 26 at 7:48.

I suggest you address that as two problems. There are several questions and answer related to both. How to load blob from MySQL See for instance Retrieve image stored as blob How to display image dynamically See for instance Show thumbnail dynamically.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions