0

I've the following code to retrieve an image from mysql and want to disply it on JSP but the web page is displayed and the image not. what is wong with this code...

 <tbody>
            <tr>
                <td> <%@ page import="java.io.*"%>
                     <%@ page import="java.sql.*"%>
                     <%@ page import="java.util.*"%>
                     <%@ page import="java.text.*"%>
                     <%@ page import="javax.servlet.*"%>
                     <%@ page import="javax.servlet.http.*"%>
                     <%@ page import="javax.servlet.http.HttpSession"%>
                     <%@ page language="java"%>
                     <%@ page session="true"%>
                     <%@ page import="java.sql.*"%>
                     <% Blob image = null; Connection con = null; Statement stmt = null; ResultSet rs = null; String iurl1=null;
                         byte[] imgData = null ; String DBname = "Rest_Tucan"; String userName = "aaks1962"; String password = "1962";
                       try {
                           Class.forName("com.mysql.jdbc.Driver");
                           con =  DriverManager.getConnection("jdbc:mysql://localhost:3306/" 
                                + DBname + "?UseUnicode=true&charachterEncoding=UTF-8");
                           stmt = con.createStatement();
                           rs = stmt.executeQuery("select * from imagesTable where imagesTableCode = 1"); 
                           while(rs.next()){
                                image = rs.getBlob("imagesTableBig");
                           }
                           // display the image
                           imgData = image.getBytes(1,(int)image.length());
                           response.setContentType("image/jpg");
                           OutputStream o = response.getOutputStream();
                           o.write(imgData);
                           o.flush();
                           o.close();

                       }catch (SQLException e) {
                            e.printStackTrace(); 
                       };
                     %>

                </td>

1 Answers1

0

Basically, you need the <img> HTML tag to make the web browser display the bytes as an image, instead of using o.write(imgData). You lack this tag in your code.

But first of all, it will be a better idea, to read the image bytes from the DB on the server side (in the Java code), then convert it to the base64 string, and finally set this string as request attribute and use the request dispatcher to pass it to the JSP page.

For example, your Java code could look like this:

(...)
request.setAttribute("imageBytes", bytesAsBase64String);
request.getRequestDispatcher("<name_of_your_page.jsp>").forward(request, response);

where bytesAsBase64String is the variable holding the result of the conversion.

Then in the JSP page you need to place the HTML tag to make the web browser display the bytes as image:

<img src="data:image/jpg;base64, ${requestScope.imageBytes}"/>

The requestScope is a built-in JSP variable which you can get the attributes from using its name. The imageBytes is the attribute name used in Servlet code.

This answer to the similar question may help you to understand the details.

EDIT: And one more thing, try keeping the @page JSP directives at the top of the JSP page to increase readability.

ptr92zet
  • 173
  • 9