Using HTML and dynamically created image in a servlet


Web tier: servlets, JSP, Web frameworks: Using HTML and dynamically created image in a servlet

  1. Hi

    I have a Java Image which I am encoding as a GIF and displaying in a Servlet by setting the content type to "image/gif". This seems to work ok.

    I now need to put HTML on the page so the gif image isn't on a blank page.

    Is it possible to used different content types in a Servlet response (wrapping out.println "text/html" content type around the image) or should I have a Frameset made up of Frames of JSP pages and the Servlet to make the page look nice?

    Any thoughts would be much appreciated.

    Thanks in advance,
    Richard Payne

  2. What exactly are you trying to do? Just show the image in an HTML page? If so, make an ImageServlet that returns the image with the correct MIME type, and then send an HTML such as this one to the client:
    <html>...<img src="../ImageServlet">...</html>

    You shouldn't try to generate both the HTML and the image in the same request.

  3. Yes, I have this same question and hope someone can answer. I am pulling an image out of a database and want to send that image to the client but not on a blank html page, I want text etc. Do I have to create a temp file to do this?
  4. Did you read my reply?
    Write a servlet that pulls the image from the DB and writes it to the stream. Be sure to set the MIME type of the response accordingly. Then, you can use this servlet just as you would use any normal image file: for instance, list it as the source in <IMG src="..."/>.