Images random creator and validator (with number and caracters) (Like hotmail)

I'm looking for something in jsp and java, that create a image ( with caracters and number), like hotmail does.

I want to create a inscription form, and at the end the new user have to reenter what he see on the create image to be able to precess the request.
This will ensure that a user calling my service and not a automatic system from someone else..

Anybody can help me..
LVL 10
Who is Participating?
It's just a matter of writing to the graphic objects before you output it to the ServletOutputStream.

I don't normally do peoples work for them but this is quite intresting. Here's a servlet that produces an image with text on. In this case, the text is defaults to "Test Me" but you could pass it through in the session or request object and validate it from there.

If you call --> Image says "Test Me"
If you call --> Image says "Banana"

as the servlet checks the request parameters for String to write into the image.

package com.bloodredsun.servlet;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * @author bloodredsun
 * @return

public class ImageServlet extends HttpServlet {

      public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

            ServletOutputStream sos = resp.getOutputStream();
            String imgString = req.getParameter("str")!=null ? req.getParameter("str") : "Test Me" ;

            // Create an image to output
            RenderedImage rendImage = myCreateImage( imgString );

            try {
                  ImageIO.write(rendImage, "jpg", sos);
            } catch (IOException e) {
                  System.out.println("Error:" + e.getMessage());

      public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            doGet(req, resp);

       * @return RenderedImage
      private RenderedImage myCreateImage( final String pString ) {
            int width = 100;
            int height = 50;

            // Create a buffered image in which to draw
            BufferedImage bufferedImage =
                  new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            // Create a graphics contents on the buffered image
            Graphics2D g2d = bufferedImage.createGraphics();

            // Draw graphics
            g2d.fillRect(0, 0, width, height);
            g2d.drawString(pString, 50, 25);

            // Graphics context no longer needed so dispose it

            return bufferedImage;

Sounds like a good idea

Here's an article showing you how to create images for servlets (and it's equally applicable for JSPs):
jabcocoAuthor Commented:
I've already read this page, but that simply explain how to genera a simple image, that not explain how to generate an image code (Image with a code text) that the user has to type in an input text. And that not explain how can i valid if the image code fit with what the user entered in the text field....

Any other idea..
Any library or util to use as a black box to generate the image? then valif if image match with the enteres text field.....

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

jabcocoAuthor Commented:
Great thanks bloodredsun..
Basic idea is there... thats what i need. now i'm gonna try to fine tuning a little bit this for more securiting image with line and things like thanks......

Big thanks!
jabcocoAuthor Commented:
But..... can i change the font style of the caractere i print.. or the color of each lettre i print... and create a kind of more hard to read image... to get a bettre security image...
>>can i change the font style of the caractere i print.. or the color of each lettre i print... and create a kind of more hard to read image... to get a bettre security image...

Yes. Draw each letter as an individual String and use these two methods to change the color and font

            g2d.setColor(Color color);
            g2d.setFont(Font font) ;

You'll have to reset the location of each string you write as they are all written in relationto  the same starting location. ALthough it might be a good idea to over-lap them slightly as auto-recognition is very tough then

>>Big thanks!
No probs dude! Just remember me when it comes to point and grades :-)
FYI: here's a class that will print out your avialable fonts:
public class GetAvailableFonts {
      public static void main (String[] args){
            // Get all font family names
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            String fontNames[] = ge.getAvailableFontFamilyNames();
            // Iterate the font family names
            for (int i=0; i<fontNames.length; i++) {
                  System.out.println(fontNames[i]) ;
jabcocoAuthor Commented:
Nice.. real thanks.. then if i want to generate "tricky line" like hotmail.. is it possible the Graphics2D?!?!

Any thing is possible in G2D. It's just not the most intuative graphics format....:-(

And what do you mean by "tricky line" exactly (I've never used hotmail)
jabcocoAuthor Commented:
Heu.. something like Yahoo resgitration or something like any other site where you need to add in a text box what caracters you see in the image, to valid your registration...
Though so, but I wanted to make sure.
jabcocoAuthor Commented:
thanks for the time you pass bloodredsun!!!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.