Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help getting image from Servlet to JSP page [duplicate]

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.

like image 828
ChronoXIII Avatar asked Jul 20 '09 15:07

ChronoXIII


1 Answers

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">
like image 150
Nick Holt Avatar answered Oct 06 '22 02:10

Nick Holt