Unless you use a "data" URI (useful for small images) the browser will make two requests: one for the HTML and one for the image. You need to be able to output an img
tag which includes enough information to let you respond to the subsequent request for the image with the data in your ByteArrayOutputStream
.
Depending on how you got that jpeg and how your server scales out, that might involve writing the image to disk, caching it in memory, regenerating it, or any combination of these.
If you can delay the image generation until the browser requests the actual image in the first place, that's pretty ideal. That may involve putting extra parameters in the URL for the image - such as points on a graph, or the size of thumbnail to generate, or whatever your image is.
If you're new to both JSP and HTML, I strongly recommend you concentrate on the HTML side first. Work out what you need to serve and what the browser will do before you work out how to serve it dynamically. Start with static pages and files for the HTML and images, and then work out how to generate them instead.