broken image when creating dynamic image in servlet

Hi all,

I am a relative new to Java. I am currently trying to port a dynamic image renderer to a servlet.

However, the final results are a broken image.

Here is what I have done:

in the web.xml

<servlet>

<servlet-name>SecurityImage</servlet-name>

<display-name>Security Image Generator</display-name>

<servlet-class>servlets.SecurityImage</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SecurityImage</servlet-name>

<url-pattern>/SecImage</url-pattern>

</servlet-mapping>

here is the class which compliled in WEB-INF/classes/servlets

package servlets;

import javax.servlet.http.*;

import java.io.*;

import javax.imageio.*;

import java.awt.*;

import java.awt.image.*;

import java.awt.geom.Rectangle2D;

publicclass SecurityImageextends HttpServlet{

privatestaticfinalint imageWidth = 200;

privatestaticfinalint imageHeigth = 75;

publicvoid doGet(HttpServletRequest request, HttpServletResponse resp){

try

{

//create an image of static propertions

BufferedImage img =new BufferedImage(imageWidth, imageHeigth, BufferedImage.TYPE_INT_ARGB);

//draw square

Graphics2D g = (Graphics2D) img.getGraphics();

g.setColor(Color.BLUE);

g.fillRect(0,0,img.getWidth(), img.getHeight());

//text to display

String imageText ="some txt";

//set up text block in Image

g.setFont(new Font("Arial", Font.BOLD, 24));

FontMetrics fontMetrics = g.getFontMetrics();

Rectangle2D rect = fontMetrics.getStringBounds(imageText, g);

//draw text in center of image

g.drawString(imageText,

(img.getWidth()- (int)rect.getWidth())/2,

(img.getHeight() - (int)rect.getHeight())/2);

//free graphic resources

g.dispose();

//set mime type of image

resp.setContentType("image/jpg");

//Write the image as jpg

OutputStream out = resp.getOutputStream();

ImageIO.write(img,"jpg", out);

out.close();

}

catch (IOException ex)

{

ex.printStackTrace();

}

}

}

could somebody give me some pointers on what went wrong?

As a test I created a text based servlet

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("Hello, world!");

out.close();

and it produced the hello world string when I execute this servlet, so the configuration must be done correctly.

Thanks for your help!

-Rogier

[4153 byte] By [rdoekesa] at [2007-11-26 23:52:36]
# 1

Glancing over it, your servlet looks ok. I'm not familiar with AWT so I would not be able to spot the problem if it's in the AWT code.

I'd suggest putting your app's logic into a standalone test class, running it from the command line to write a file to the filesystem. If the image doesn't open properly then you need to fix that part of your app first.

If the image is generated properly, then it's something with the servlet; again though, it looks ok to me.

Have you checked to make sure you're not getting an exception?

bigtangringoa at 2007-7-11 15:32:37 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Thanks bigtangingo for your reply.

Per your advise I narrowed it down some. Running it from the command line and writing it to the file system works very well, even with the expected results.

my app server does not throw exceptions, so that's no help either.

I checked my classpath and I am using the servlet.jar implementation version 2.3.1.

Any known problems with this?

Again, thank again for all help

-Rogier

rdoekesa at 2007-7-11 15:32:37 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Take a look at this servlet and see if it helps: http://rsb.info.nih.gov:8080/SampleServlet.java.txt
bigtangringoa at 2007-7-11 15:32:37 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Well after (a lot of) trial and error I narrowed the problem down even more..

When I changed the content type to PNG and the ImageIO writing

resp.setContentType("image/png");

ImageIO.write(img, "png", (OutputStream) resp.getOutputStream());

the image displayed fine.

Does anybody know if there is a problem rendering jpg images dynamically?

Thanks,

-Rogier

rdoekesa at 2007-7-11 15:32:37 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
i am using same code, but i can only see blue screen, without any text printed on it. Please help me what could be a problem.
waqasahmedcha at 2007-7-11 15:32:37 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...