Hi,
I store images in my MYSQL database. I need to retrieve that image to my jsp along with datas.Response.write() function only displays the picture and not the data. How to do that?
Hi,
I store images in my MYSQL database. I need to retrieve that image to my jsp along with datas.Response.write() function only displays the picture and not the data. How to do that?
The behavior seen in 's sample is normal: once the response is switched to an image MIME type and raw bytes are written, the browser treats the whole HTTP response as an image. That prevents any surrounding HTML or other data from rendering. Mixing text output (JSP writer) and a binary image stream in one response also leads to problems (and can throw an IllegalStateException).
A common, robust pattern is to serve the BLOB as a separate resource and reference it from the HTML page. The page that shows student data should render normal HTML (name, fields, etc.) and include an <img> tag whose src points to a dedicated servlet (or mapped JSP) that streams only the image bytes. The image endpoint must set the correct Content-Type, stream the BLOB, return 404 or a placeholder if missing, and close DB/resources in a finally block (or use try-with-resources). Avoid calling both response.getWriter() and response.getOutputStream() in the same request.
Minimal servlet pattern (Java 8+/9+ style):
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
resp.setContentType("image/jpeg");
try (Connection c = ds.getConnection();
PreparedStatement ps = c.prepareStatement("SELECT image FROM student WHERE id=?")) {
ps.setString(1, id);
try (ResultSet rs = ps.executeQuery()) {
if (!rs.next()) { resp.sendError(HttpServletResponse.SC_NOT_FOUND); return; }
try (InputStream in = rs.getBinaryStream("image");
ServletOutputStream out = resp.getOutputStream()) {
in.transferTo(out); // simple copy; use manual loop if older Java
}
}
} catch (SQLException e) {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
} Alternate: embed small images inline with Base64 in the HTML (not recommended for large BLOBs):
String base64 = Base64.getEncoder().encodeToString(bytes);
out.println("<img src=\"data:image/jpeg;base64," + base64 + "\">"); Troubleshooting notes for and others: use browser devtools Network tab to inspect the image request and response headers; confirm the DB column contains data; check servlet mapping and server logs for exceptions; avoid response.reset() unless intentionally clearing prior output; and always close DB resources.
Jump to Post— peter_budo 2,532why is it that the code above displays only a blank page??
Not sure what you are talking about? Which code?
Hi
i use the following code to retrieve image which displays only image. not any other datas.
Connection connection = null;
//login is the name of the database
String connectionURL = "jdbc:mysql://localhost:3306/login";
ResultSet rs = null;
PreparedStatement psmnt = null;
InputStream sImage;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
//Student is the table name
psmnt = connection.prepareStatement("SELECT image FROM student WHERE id = ?");
//In id "6" i have the image.
psmnt.setString(1, "6");
rs = psmnt.executeQuery();
if(rs.next())
{
byte[] bytearray = new byte[1048576];
int size=0;
sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/jpeg");
while((size=sImage.read(bytearray))!= -1 )
{
response.getOutputStream().write(bytearray,0,size);
}
response.flushBuffer();
sImage.close();
rs.close();
}
}
catch(Exception ex)
{
out.println(ex);
}
psmnt.close();
connection.close(); while((size=sImage.read(bytearray))!= -1 ) { response.getOutputStream().write(bytearray,0,size); } response.flushBuffer(); sImage.close(); rs.close(); } } catch(Exception ex) { out.println(ex); } psmnt.close(); connection.close();
while((size=sImage.read(bytearray))!= -1 ) { response.getOutputStream().write(bytearray,0,size); } response.flushBuffer(); sImage.close(); rs.close(); } } catch(Exception ex) { out.println(ex); } psmnt.close(); connection.close();
why is it that the code above displays only a blank page??
why is it that the code above displays only a blank page??
Not sure what you are talking about? Which code?
the one that is longer.. it only displays a blank page and no image..
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.