Captcha Source Code using JAVA JSP Struts and Servlet

Captcha is very important thing to prevent spam. Some spammers use some software to detect free forum to spam. Usually there aim is to publish Ads or motivated to gain money. So to prevent this kind of attacks programmer developed captcha. 

What is Captcha?
Captcha is a human readable code which is used to verify whether the person is really a human or some softwares like spam. 

How Captcha Works?
Captcha works in dynamic method runtime. So by using this technique it randomly produce some codes and verify whether the code is correct or not.

Personally me and my team have developed this code using Struts also servlet. You can utilize this code to develope a own Captcha without using any third party tools like Implementing this is very difficult task also this is third party tool. So no need of any worries just put this below code in any of your forum or validation it will automatically check whether you enter the correct captcha code or not. Its simple to use and user friendly.

This source code is developed using Java, JSP, Struts and Servlet.

Here is the Code to Develope your own Captcha without any third party tool.
Its Copy right code. Don't use for commercial Purpose without programmer Permission


import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.*;
import javax.servlet.http.*;

public class CaptchaServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

int width = 150;
int height = 50;

char data[][] = {
BufferedImage bufferedImage = new BufferedImage(width, height,
Graphics2D g2d = bufferedImage.createGraphics();
Font font = new Font("Georgia", Font.BOLD, 18);
RenderingHints rh = new RenderingHints(
GradientPaint gp = new GradientPaint(0, 0,
Color.magenta, 0, height/2,, true);
g2d.fillRect(0, 0, width, height);
g2d.setColor(new Color(221, 233, 55));
Random r = new Random();
// int index1 = Math.abs(r.nextInt());
// int index=index1%10;
Random randomGenerator = new Random();
int index = randomGenerator.nextInt(999);
String captcha = String.copyValueOf(data[index]);
request.getSession().setAttribute("captcha", captcha );
int x = 0;
int y = 0;
for (int i=0; i
x += 10 + (Math.abs(r.nextInt()) % 15);
y = 20 + Math.abs(r.nextInt()) % 20;
g2d.drawChars(data[index], i, 1, x, y);
OutputStream os = response.getOutputStream();
ImageIO.write(bufferedImage, "png", os);
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);

The above code is java code just use IDE to implement this

The below is Scriptlet for JSP just implement into your jsp page.

<% String captcha = (String) session.getAttribute("captcha"); String code = (String) request.getParameter("code"); if (captcha != null && code != null) { if (captcha.equalsIgnoreCase(code)) { String url="#"; RequestDispatcher rd=request.getRequestDispatcher(url); rd.forward(request,response); } else { out.print("
The characters you entered didn't match the word verification. Please try again.

Copying, Editing or Republishing this article without author permission has been strictly prohibited under Judicial act.

Thanks ,
Kamalakannan JS

  1. Anonymous December 14, 2010 at 3:23 PM  

    (Math.abs(r.nextInt()) % 15) should be written as r.nextInt(15)