What I am trying to do is basically to display image from Mysql on jsp page. I was successfully able to call the blob image file into outputstream in my java servlet using the following code.
In servlet,
byte[] imgData = null;
Blob image1 = null;
image1 = result.getBlob("image1");
imgData = image1.getBytes(1, (int)image1.length());
In jsp,
<%
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
o.write(imgData);
%>
The problem with this method is that this jsp page is fulfilled with the image. That way, the page discards other contents and only displays the image. What I want to do is to display few images using
<img src=??? width=??? height=???>
I tried to resize output stream image, but I don't think there is a way.
Please help me
Thanks in advance.
In my opinion, you should create two different servlets:
One for downloading an image by getting a writer from HttpServlet
and writing byte array as a response.
Two, the .jsp
page will contain an img
tag which src
will link to the first endpoint.
This will look something like:
1)
public class ImageServlet extends HttpServlet {
//...
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
//...
response.setHeader("Content-Type", this.getServletContext().getMimeType(imageFileName));
response.setHeader("Content-Length", X);
response.getOutputStream().write(<<image byte[]>>);
}
2)
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<html>
<head><title>Hello</title></head>
<body bgcolor="white">
<img src="/images?id=3" height="Xpx" width="Ypx">
<h2>My name is Duke. What is yours?</h2>
</body>
</html>
I think this should work. If the answer was helpful, vote as an accepted answer please.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.