Encoding problems...

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Raul Escobedo

Encoding problems...

Post by Raul Escobedo » Fri Nov 17, 2000 3:19 am

Im am using JFreeChart and I want to encode the image using the ACME encoder to output a chart to a web page. When the image is done encoding I get the encoded code displayed on the page instead of the Image itself. I have followed all of your suggestions but they have not worked for me. I am not using an applet, I want to do it from a servlet (.jsp) file. What should my HTML file look like, am I missing a step after the encoding is done? As you can see I am really confused and out of ideas as to what to do? Can someone please help me...

Thanks

<%
BufferedImage image = new BufferedImage(500,500,Buffered_Image.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.green);
g.fillRect(0,0,10,10);
ServletOutputStream outs = response.getOutputStream();
new GifEncoder(image,outs).encode();
outs.close();
%>

This is just the code that I am using in a .jsp file to try to find my problem without having to worry about using JFreeChart yet...

Matt

RE: Encoding problems...

Post by Matt » Fri Nov 17, 2000 11:05 pm

Raul,

I think the problem here is you are sending data to the OutputStream, but it is already set to show content of type text/html by the jsp. Since the result of the encode() operation is outputting an image/gif, this won't show up in your browser. jsps are just turned into servlet code by the servlet engine, then compiled into class files. You don't get to set the content type of the response.

If you want to use a servlet to show an image, you have basically two choices. You can write a servlet (not a jsp) that sets the content type to image/gif, then writes the image to the outputstream, or you can write the image to a directory that the web server has access to using a java servlet (or a java program, or bean for that matter), then write out html with an <img src="path to image"> tag.

To see some code that does the former, get the O'Reilly book "Java Servlet Programming" by Jason Hunter for some samples.

I'm doing the latter right now, but am working on some ways to make it easier, but don't have anything to give you right now that would really help.

Hope the information helps a bit...

Matt

Glenn Wilson

RE: Encoding problems...

Post by Glenn Wilson » Thu Dec 07, 2000 4:19 am

This is the code you'd need to add to set the content type.
response.setContentType("image/gif");

If you want a general explanation, check out this response from
Hot Dispatch:

http://www.hotdispatch.com/provider-vie ... 3185147926

Raul Escobedo

RE: Encoding problems...

Post by Raul Escobedo » Thu Dec 07, 2000 5:20 pm

Thanks Matt actually you guessed correctly as to what the problem was, I did need to use servlets instead of just a jsp. Also I noticed that servers require you to register your servlets in order to use them (Apache, Weblogic). Other than that the graphs look great.

Martin Smith

RE: Encoding problems...

Post by Martin Smith » Sat Dec 30, 2000 12:32 am

Matt--

I'm cruising around looking for chart components for JSP environments (JRun and Apache/Tomcat.) This thread looks pretty relevant.

I'd love to see something all neatly tied up in a TAGLIB . . .

But it's 360 days 'till Christmas, I know. <g>

I'll check back in a while.

Martin

PS--I'd volunteer to try making this myself but I'm not that good and never fooled with images at all . . .

Locked