Java – display blob (image) through JSP
I have a code to display employee charts
Data (name, phone, photo, etc.) are stored in SQL server and displayed through JSP Display data can be, except for images Jpg (stored in the image = blob column)
By the way, I've shown the image (see the code below), but I don't know how to put it in The area defined in CSS (see the code below), because the image is loaded into the whole browser page through resultset
Does anyone know how I "draw" images?
<% Connection con = FactoryConnection_sql_SERVER.getConnection("empCHART"); Statement stSuper = con.createStatement(); Statement stSetor = con.createStatement(); Blob image = null; byte[] imgData = null; ResultSet RSSuper = stSuper.executeQuery("SELECT * FROM funChart WHERE dept = 'myDept'"); if (RSSuper.next()) { image = RSSuper.getBlob(12); imgData = image.getBytes(1,(int) image.length()); response.setContentType("image/gif"); OutputStream o = response.getOutputStream(); //o.write(imgData); // even here we got the same as below. //o.flush(); //o.close(); --[...] <table style="margin: 0px; margin-top: 15px;"> <tr> <td id="photo"> <img title="<%=RSSuper.getString("empName").trim()%>" src="<%=%20o.wite(imageData);%20o.flush();%20o.close();%20%>" /> </td> </td> <td id="empData"> <h3><%=RSSuper.getString("empName")%></h3> <p><%=RSSuper.getString("Position")%></p> <p>Id:<br/><%=RSSuper.getString("id")%></p> <p>Phone:<br/><%=RSSuper.getString("Phone")%></p> <p>E-Mail:<br/><%=RSSuper.getString("Email")%></p> </td> </table>
Here is the image of the clip:
#photo { padding: 0px; vertical-align: middle; text-align: center; width: 170px; height: 220px; }
Thank you in advance!
Solution
You have made some fundamental mistakes here< IMG SRC > must point to a URL that does not contain the binary content of the image The content type of the JSP page itself should not be set to image / GIF It should remain default to text / HTML It is not correct that the web server should include specific images in HTML results in the way you expect This is the image that the WebBrowser downloads separately according to the URL found in the SRC attribute, and then renders it accordingly
The simplest is to create a separate servlet to stream the image from the database to the response body You can uniquely identify the image by requesting parameters or path information This is an example of using the request parameter:
<img src="imageServlet?id=<%=RSSuper.getString("id")%>" />
Then the doget () method should basically perform this work:
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.
Regardless of specific issues, the use of scriptlets in this way has been formally and strongly discouraged for a decade Maybe you're reading completely old books / tutorials, or maintaining an old JSP Web application For some insights, please refer to the answers to the following questions. There are some tips:
> 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?