Hi
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.

Thanks.

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:

<html>
<head><title>Hi</title></head>
<body>
  <img src="/yourapp/img/img1" /><br/>
  <img src="/yourapp/img/img2" /><br/>
  <img src="/yourapp/img/img3" /><br/>
  <img src="/yourapp/img/img4" /><br/>
</body>
</html>

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);
	setBuffImage(bi);
	....				
		
         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.
Thanks.

`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);
		response.setContentType("img/png");
		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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.