Help getting image from Servlet to JSP page [duplicate]

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="https://stackoverflow.com/questions/1154254/url to your servlet">

Leave a Comment