Only JSP
<%
BufferedImage bImage = ImageIO.read(new File(“/home/mohit/Desktop/mohit.jpg”));//give the path of an image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bImage, “jpg”, baos );
baos.flush();
byte[] imageInByteArray = baos.toByteArray();
baos.close();
String b64 = javax.xml.bind.DatatypeConverter.printBase64Binary(imageInByteArray);
%>
<img src=”data:image/jpg;base64, <%=b64%>” alt=”Image not found” />
When buffered image is passed to JSP as session attribute
Servlet
request.getSession().setAttribute(“bufferedimage”, bufferedimage);
JSP
<%
BufferedImage bImage = (BufferedImage) session.getAttribute(“bufferedimage”);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bImage, “jpg”, baos );
baos.flush();
byte[] imageInByteArray = baos.toByteArray();
baos.close();
String b64 = javax.xml.bind.DatatypeConverter.printBase64Binary(imageInByteArray);
%>
<img src=”data:image/jpg;base64, <%=b64%>” alt=”Image not found” />
When buffered image stream is passed to JSP as session attribute
Servlet
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedimage, “jpg”, baos );
baos.flush();
byte[] imageInByteArray = baos.toByteArray();
baos.close();
String b64 = javax.xml.bind.DatatypeConverter.printBase64Binary(imageInByteArray);
request.getSession().setAttribute(“b64”, b64);
JSP
<img src=”data:image/jpg;base64, <%=b64%>” alt=”Image not found” />