I have to display 4 BufferedImage in jsp page.I am getting these images from a java code. In the jsp page i am using:

ImageIO.write(demo.getBuffImage(), "png", response.getOutputStream());

With this i am able to get the image. However if i write different ImageIO.write(....) for displaying each of the 4 images, I still get only the first image and not the rest.
I am new to jsp. Please give me some solution.


7 Years
Discussion Span
Last Post by ~s.o.s~

Without seeing the code, it would be difficult to tell but are you updating your `demo` object so that it always fetches a new buffered image?

Also, this approach isn't a good one. You can at a given time write out only a single type of entity (to put it in simple terms); which is the reason why you set the content type of your writer/outputstream before writing out the response. A better solution here would be to have four IMG tags on your web page having different SRC values. For each SRC value, an appropriate byte stream would be retrieved for a given image and displayed to the browser.

Something like:

  <img src="/yourapp/img/img1" /><br/>
  <img src="/yourapp/img/img2" /><br/>
  <img src="/yourapp/img/img3" /><br/>
  <img src="/yourapp/img/img4" /><br/>

Edited by ~s.o.s~: n/a


Thanks for replying.
I actually want to displays graphs. I am using the jfree chart library-timeseries. in my jsp page the user is supposed to enter the start and end date.I have four different java classes which will give my four different graphs for the same entered dates. The submit button will take me to the next page where the 4 graphs are to be displayed.
In my java code, it is like:

XYDataset dataset = createDataset(startDate,endDate);
	JFreeChart chart = createChart(dataset);
	BufferedImage bi = chart.createBufferedImage(900, 320);
         public void setBuffImage(BufferedImage buffImage) {
	    this.buffImage = buffImage;

	public BufferedImage getBuffImage() {
	     return buffImage;

Then in the jsp page i m writing: ImageIO.write(demo.getBuffImage(), "png", response.getOutputStream()); U have suggested to use img tag.But will you plz tell me how i m supposed to write it. <img src="/yourapp/img/img1" /><br/> in place of yourapp wht m i supposed to do and how.

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)


`yourapp` is the name of your web application. You need to create a controller/servlet which accepts some input parameters and generates the images on fly which are then sent to the client. A simple example would be:

public class ImageServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse res)
		throws IOException, ServletException {
		String graphType = request.getParamter("type");
		// read other data like graph parameters
		BufferedImage img = generateImage(graphType);
		OutputStream os = response.getOutputStream();
		// write data from BufferedImage to the servlet output stream

In you deployment descriptor, map this Servlet against the path "/img". You can then use this servlet like:

<img src="/MyWebApplication/img?type=first" /><br />
<img src="/MyWebApplication/img?type=second" /><br />
<img src="/MyWebApplication/img?type=third" /><br />

Oh and when posting code the next time, use code tags. Failure to do so would result in your post being ignored by the regulars here; read forum rules for more details.

Edited by ~s.o.s~: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.