Junk Out put when displaying Image thorough servle

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

Junk Out put when displaying Image thorough servle

Post by San » Tue Jan 16, 2001 6:02 pm

Hi I am kind of new to displaying Image throught servlets . I tried to run the following sample code . But when I run the servlet Junk data comes on the browser .
Any sort of help will be great .
Thanks in advance .

// Sample code
import java.io.*;
import java.awt.*;
import javax.servlet.*;
import javax.servlet.http.*;

import Acme.JPM.Encoders.GifEncoder;

public class HelloWorldGraphics extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ServletOutputStream out = res.getOutputStream(); // binary output!

Frame frame = null;
Graphics g = null;

try {
// Create an unshown frame
frame = new Frame();
frame.addNotify();

// Get a graphics region, using the Frame
Image image = frame.createImage(400, 60);
g = image.getGraphics();

// Draw "Hello World!" to the off screen graphics context
g.setFont(new Font("Serif", Font.ITALIC, 48));
g.drawString("Hello World!", 10, 50);

// Encode the off screen image into a GIF and send it to the client
res.setContentType("image/gif");
GifEncoder encoder = new GifEncoder(image, out);
encoder.encode();
}
finally {
// Clean up resources
if (g != null) g.dispose();
if (frame != null) frame.removeNotify();
}
}
}

Raul Escobedo

RE: Junk Out put when displaying Image thorough se

Post by Raul Escobedo » Tue Jan 30, 2001 11:53 pm

Try putting your code in the service section of your servlet...

Locked