0

I have an image in byte representation and I need renders this image to JSP page. I send the byte array to JSP through servlet.getOutputStream(). How draw my image in a browser?

I send my byte[] :

byte[] image = getDAO().getImage(propose.getId());

ServletOutputStream os = resp.getOutputStream();
os.write(image);
os.close();

How to render the image to JSP?

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    ...Somehow render get byte and render image ...
</body>
</html>
Pavel
  • 2,005
  • 5
  • 36
  • 68

1 Answers1

1

You can just use the <img> tag in your JSP and simply map it to the URL of the servlet providing the image, like this:

<img src="urlOfYourServlet">
Luka
  • 26
  • 1
  • 5
  • it's work if I sending more different data? I send image and another text in JSP. – Pavel Jul 30 '17 at 12:04
  • You should use the servlet's output stream only to write one set of data. If you need to send some text to the JSP, introduce another servlet who will take care of that. – Luka Jul 30 '17 at 14:14
  • But this is two requests instead one, different for image and text data. Or are You mean using include()? – Pavel Jul 30 '17 at 14:22
  • Sorry, I'm not familiar with the include method. What I meant to say is that, if you need to send different data types to your JSP, you should use one servlet for each of the data types. For example, you could have an ImageServlet which just sends an array of bytes to the JSP, and a TextServlet sending some text to that same JSP. Then, in the JSP you simply map to these two servlets to fetch the data. I hope this clears things up a little bit; if not, feel free to ask whatever's hazy. – Luka Jul 30 '17 at 16:19