How to display an image in jsp?
Solution 1:
The HTML you generate in your JSP must contain an img element with an src pointing to the URL of a servlet or action which will load the image from the database and send it to the ouput stream with the image/jpeg content type.
// in your HTML :
<img src="/getImage.action?imageId=${id_of_the_image}"/>
// in the servlet mapped to /getImage.action:
// get the ID of the image from the request parameters
String imageId = request.getParameter("imageId");
byte[] imageData = getImageFromDatabase(imageId);
response.setContentType("image/jpeg");
response.getOutputStream().write(imageData);
All the browsers have a right-click - Save image as... menu item, so I wouldn't implement this in the app.
Solution 2:
JSP:
<div id="profileDiv" style="padding: 10px; border: solid 2px #D6D6D6;">
<img src="imageDisplayProcess.do?pKey=<c:out value="${staff.staffId}" />"
width="117" height="160"
onError="loadImage()" onAbort="loadImage()" />
</div>
Servlet //imageDisplayProcess
imgByt = imageClass.getPhotograph();//return blob...
response.setContentType("image/jpg");
response.getOutputStream().write(imgByt);
response.getOutputStream().flush();
response.getOutputStream().close();