views:

4687

answers:

4

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.

+1  A: 

If I understand your problem correctly, the sequence of events will be:

  1. You generate an HTML page;
  2. That HTML page is sent to the client; and
  3. The client's browser reads the image URL and requests it as a separate request.

So, you can't generate the image and pass it to the JSP. You can however generate a URL to get the image and put that in the JSP. That's easy enough to pass by the servlet putting it on the HttpServletRequest object (request scope in JSP). For example, generate:

<a href="http://myhost.com/image_servlet?id=1234"/&gt;

You don't really say what that text is or what information is required to generate the image. If you can't encapsulate that in a GET URL, you may need to add extra information and put it in the HttpSession so it can be retrieved on the next get image request.

cletus
+7  A: 

You need 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">
Nick Holt
What code would i have in the jsp page that would display the image
ChronoXIII
Something like <img src="url to your servlet"/>
Nick Holt
+3  A: 

You can't1 return both in the same response, since you're returning different types (an HTML page of type text/html and an image of type image/jpeg, say).

For this sort of thing, I will generate the image during the initial servlet request (for the containing HTML page). I store it in a cache in my servlet, and write the HTML page with the image tag containing a URL to that image with the handle.

e.g. the browser asks for http://whatever/page

The servlet generates the image, and writes an HTML tag in the page like

<img src="http://whatever/image/unique_handle_to_image"&gt;

The browser will render the HTML page, and as part of that issue a new request to my servlet with the handle for the image.

e.g. the browser now asks for http://whatever/image/unique_handle_to_image

I then return the image as content type image/jpeg or similar.

So you have two requests going on. One for the page, in which you render the image and store it temporarily, and the second in which you return the image. You have to remember to clear the image cache, but that's all straightforward. I wouldn't worry about storing lots of images, since the two requests from the browser usually (!) come in quick succession.

  1. I guess it's possible to use a data uri provided your browser supports it, and create something like

    <img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0 vr4MkhoXe0rZigAAAABJRU5ErkJggg==" alt="Red dot" />

Note there are a number of caveats surrounding these. See the linked page.

Brian Agnew
+1  A: 

I would do something along this lines to achieve this:

On the JSP page you put a link to an image:

<img src="servlet/path?word=value">the rest</img>

This link points to your servlet, it generates image using request parameters, you do not need to save it, just put it right into response's output stream. You have to remember to disable browser caching for this servlet.

JSP page is displayed first, next all the images are requested, it should work just fine.

Of course, you should not put the text to display in a parameter like this, you should propably encipher it somehow or store it in a HTTP session.

Hope this helps.

Ula Krukar