Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to send Graphics using a servlet?

0 views
Skip to first unread message

Thomas Kern

unread,
Aug 17, 2000, 3:00:00 AM8/17/00
to
Hi,
I try to generate a HTML-page in my servlet containing a GIF graphics.
However, if I use following lines in my doGet()-method I get a "Internal
Server Error" message in my browser.
BufferedImage img = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics(); // this causes the error
I'd need a Graphics object to paint my picture on, how do i do that in a
servlet? In my application everything worked fine ...
Thanx
tom.

Jon Skeet

unread,
Aug 17, 2000, 3:00:00 AM8/17/00
to

What's your servlet environment? Whenever you get an internal server
error, you should consult the logs. My guess is that in this case, you've
got a Unix servlet engine running without an X server attached. You'll
need to run an X server (virtual or not), or change AWT implementation,
in order to use any graphics.

--
Jon Skeet - sk...@pobox.com
http://www.pobox.com/~skeet/

Thomas Kern

unread,
Aug 17, 2000, 3:00:00 AM8/17/00
to

Jon Skeet <sk...@pobox.com> schrieb in im Newsbeitrag:
MPG.1405e5cf888597ef98a5f9@news...

yes, you are right. that seems to be the problem. i am using a LINUX
machine, but a x-server is installed, since the graphical environment can be
started.
the question now is how to configure the x-server (or java), so that it
works. any idea?
lg
tom

Jon Skeet

unread,
Aug 17, 2000, 3:00:00 AM8/17/00
to
Thoma...@gmx.at wrote:
> yes, you are right. that seems to be the problem. i am using a LINUX
> machine, but a x-server is installed, since the graphical environment can be
> started.
> the question now is how to configure the x-server (or java), so that it
> works. any idea?

Probably just starting the X server is enough, and set the DISPLAY
environment variable in the shell that you run the servlet engine under.
(That's probably the tricky bit.)

Thomas Kern

unread,
Aug 17, 2000, 3:00:00 AM8/17/00
to
> Probably just starting the X server is enough, and set the DISPLAY
> environment variable in the shell that you run the servlet engine under.

that problem seems fixed.
my jserv-log contains this message now:
java.lang.NoClassDefFoundError: sun/awt/X11GraphicsEnvironment
at
org.apache.jserv.JServConnection.processRequest(JServConnection.java:323)
at org.apache.jserv.JServConnection.run(JServConnection.java:188)
at java.lang.Thread.run(Thread.java:475)
what am i missing?
tom.

Jon Skeet

unread,
Aug 17, 2000, 3:00:00 AM8/17/00
to
Thomas Kern<Thoma...@gmx.at> wrote...

Sounds like you're missing part of the JRE - namely the awt part. See if
you can run a normal GUI app from X using the same jre.

--
Jon Skeet
sk...@pobox.com - http://www.pobox.com/~skeet

David Balme

unread,
Sep 2, 2000, 10:55:43 AM9/2/00
to
The way I seen this done ( and the way I have copied this ;-) ) is to
create a frame object
and from there draw your image.. see code snippet.

import Acme.JPM.Encoders.*;
:
Frame container = new Frame();
try{
container.addNotify();

container.add(button);
Image img = null;
img = container.createImage(gifWidth,gifHeight+20);
Graphics g = img.getGraphics();

: Draw your image

ServletOutputStream out = response.getOutputStream();
GifEncoder gif = new GifEncoder(img,out);
gif.encode();
out.flush();
}catch(Exception ex){
ex.printStackTrace();
}finally{
container.dispose();
}

Notice my use of new GifEncoder(img,out);

I grabbed this gif encoder curtesy of http://www.acme.com/. There's a lot
of free code samples and class libraries at this site. Mostly written by
one guy I think.. Very prolific and capable person.

Cheers and good luck.

David Balme

Thomas Kern wrote:

> Hi,


> I try to generate a HTML-page in my servlet containing a GIF graphics.
> However, if I use following lines in my doGet()-method I get a "Internal
> Server Error" message in my browser.
> BufferedImage img = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
> Graphics g = img.getGraphics(); // this causes the error
> I'd need a Graphics object to paint my picture on, how do i do that in a
> servlet? In my application everything worked fine ...

> Thanx
> tom.

0 new messages