Help getting image from Servlet to JSP page

image, java, jsp, servlets

Solution

You need to write the image as a byte array to the response's output stream. Something like this:

byte[] imageBytes = getImageAsBytes();

response.setContentType("image/jpeg");
response.setContentLength(imageBytes.length);

response.getOutputStream().write(imageBytes);

Then in you JSP you just use a standard `img` element:

<img src="url to your servlet">

Problem

I currently have to generate an image that displays the text of a string, i need to make this image on a Servlet and then somehow pass the image to a JSP page so that it can display it. I'm trying to avoid saving the image, and instead somehow stream the image to the JSP. I haven't found a way of generating the image since i started with finding how to pass an image from the Servlet to the JSP adn got stuck. EDIT: The jsp page is already made and isn't created by the servlet, i have to pass the image into an already existing jsp Any help is appreciated.

Original source

Related problems