You can create a servlet that renders the image from database.
Here's the code:
Code:
public class ViewImageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
byte[] img = null;
if (request.getParameter("id") != null) {
Integer id = Integer.parseInt(request.getParameter("id"));
DAO dao = new DAO();
MyOBJ obj = dao.getObj(id);
img = obj.getImage();
}
if (img == null || img.length == 0) {
String fileName = getServletContext().getRealPath(
"/noimage.gif");
File file = new File(fileName);
FileInputStream in = new FileInputStream(file);
img = new byte[(int) file.length()];
in.read(img);
in.close();
}
response.setContentLength(img.length);
response.setContentType("image/jpeg");
OutputStream out = response.getOutputStream();
out.write(img);
out.flush();
out.close();
} catch (DAOException e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
When you wanna render it to browser you simply call or put in src attribute from image the following address (for example):
http://localhost:8080/myproject/viewImage?id=1