Hi Ankit, Maybe, I can help you.
I write below the code than I use to do Captcha check. In the form panel: ######### FormPanel form = new FormPanel(); .... VerticalPanel vp = new VerticalPanel(); HorizontalPanel hp = new HorizontalPanel(); hp.setSpacing(10); codeCheck = new TextField<String>(); codeCheck.setFieldLabel("Controllo"); codeCheck.setAllowBlank(false); verifyImage = new Image("verify.png"); hp.add(verifyImage); hp.setCellHorizontalAlignment(verifyImage, HasAlignment.ALIGN_RIGHT); hp.setCellWidth(verifyImage, "170px"); final Button clickImage = new Button("New image"); clickImage.addSelectionListener(new SelectionListener<ButtonEvent>() { @Override public void componentSelected(ButtonEvent ce) { codeCheck.setValue(""); int rand = Random.nextInt(); String url = "verify.png?rand=" + rand; verifyImage.setUrl(url); } }); hp.add(clickImage); vp.add(hp); HTML info = new HTML("Write in the check field the sequence of characters in the image."); info.setStyleName("x-label"); vp.add(info); panel.add(vp); panel.add(codeCheck); errorCaptcha = new HTML(""); errorCaptcha.setStyleName("x-label"); errorCaptcha.addStyleName("x-label-red"); vp.add(errorCaptcha); Button btn = new Button("Check"); btn.addSelectionListener(new SelectionListener<ButtonEvent>() { @Override public void componentSelected(ButtonEvent ce) { if (!panel.isValid() || codeCheck.getValue()==null) { return; } service.checkCode(codeCheck.getValue(), new AsyncCallback<String>() { public void onFailure(Throwable caught) { MessageBox.alert("error",caught.getMessage(),null); } public void onSuccess(String result) { if (((String)result).equals("Sorry please enter correct code for verification.")){ errorCaptcha.setHTML(result); return; } else { errorCaptcha.setHTML(""); } } }); // panel.submit(); // this is your form submit } }); panel.addButton(btn); ############## Now, the GWT RPC servlet code (it's very simple): ############## public String checkCode(String entercode) { session = getThreadLocalRequest().getSession(); String code = (String)session.getAttribute("verification.code"); String attempt = entercode; String msg = ""; if (code!=null && code.equals(attempt)){ msg = "Correct verification code"; } else { msg = "Sorry please enter correct code for verification."; } return msg; } ############## In the reality the url "verify.png" for the image is a servlet, and you must define this into web.xml: ############## <servlet> <servlet-name>VerificationServlet</servlet-name> <servlet-class>com.yourpackage.server.VerificationServlet</servlet- class> </servlet> <servlet-mapping> <servlet-name>VerificationServlet</servlet-name> <url-pattern>/verify.png</url-pattern> </servlet-mapping> ############## Now the code about VerificationServlet: ############## package com.yourpackage.server; import java.io.*; import javax.servlet.ServletContext; import javax.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class VerificationServlet extends HttpServlet { private static final long serialVersionUID = 1L; HttpSession session; ServletContext context = null; public void init(ServletConfig config) throws ServletException { super.init(config); context = config.getServletContext(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { session = request.getSession(true); response.setHeader("Pragma", "public"); response.setHeader("cache-control", "max-age=0,no-cache, no- store,must-revalidate, proxy-revalidate, s-maxage=0"); response.setDateHeader("Expires", 0); response.setContentType("image/png"); ImageVerification verification = new ImageVerification(response.getOutputStream()); String code = verification.getVerificationValue(); session.setAttribute("verification.code", code); response.flushBuffer(); } } ############## And the last is the code about ImageVerification: ############## package com.yourpackage.server; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.util.Random; import java.util.UUID; import javax.imageio.ImageIO; /** * ImageVerification is a simple utility class for * creating an image verification PNG file that will * allow you to make sure that only a human can read * the alphanumeric values and enter them into a text * field during verification. <P> * */ public class ImageVerification { private String value; public ImageVerification (OutputStream out) throws IOException { this(50,120,out); } public ImageVerification (int height, int width, OutputStream out) throws IOException { BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Random rand=new Random(System.currentTimeMillis()); Graphics2D g = bimage.createGraphics(); // create a random color Color color = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)); // the the background to the random color to fill the // background and make it darker g.setColor(color.darker()); g.fillRect(0, 0, width, height); // set the font g.setFont(new Font("arial",Font.BOLD,36)); // generate a random value this.value = UUID.randomUUID().toString().replace("-","").substring(0,5); int w = (g.getFontMetrics()).stringWidth(value); int d = (g.getFontMetrics()).getDescent(); int a = (g.getFontMetrics()).getMaxAscent(); int x = 0, y =0; // randomly set the color and draw some straight lines through it for (int i = 0; i < height; i += 5) { g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255))); g.drawLine(x, y + i, width, y+i); } // reset x and y x=0; y=0; // randomly set the color of the lines and just draw think at an angle for (int i = 0; i < height; i += 5) { g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255))); g.drawLine(x, y + d - i, width + w, height + d - i); } // randomly set the color and make it really bright for more readability g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)).brighter().brighter()); // we need to position the text in the center of the box x = width/2 - w/2; y = height/2 + a/2 - 2; // affine transform is used to rock the text a bit AffineTransform fontAT = new AffineTransform(); int xp = x-2; // walk through each character and rotate it randomly for (int c=0;c<value.length();c++) { // apply a random radian either left or right (left is half since it's too far back) int rotate = rand.nextInt(20); fontAT.rotate(rand.nextBoolean() ? Math.toRadians(rotate) : -Math.toRadians(rotate/2)); Font fx = new Font("arial", Font.BOLD, 36).deriveFont(fontAT); g.setFont(fx); String ch = String.valueOf(value.charAt(c)); int ht = rand.nextInt(3); // draw the string and move the y either up or down slightly g.drawString(ch, xp, y + (rand.nextBoolean()?-ht:ht)); // move our pointer xp+=g.getFontMetrics().stringWidth(ch) + 2; } // write out the PNG file ImageIO.write(bimage, "png", out); //System.out.println("DONE WRITYING NEW IMAGHE"); // make sure your clean up the graphics object g.dispose(); } /** * return the value to check for when the user enters it in. Make sure you * store this off in the session or something like a database and NOT in the * form of the webpage since the whole point of this exercise is to ensure that * only humans and not machines are entering the data. * * @return */ public String getVerificationValue () { return this.value; } } ################ Ok, this is all. I hope this could help you. Hi Carlo -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.