Captcha in JSP and Servlet

Note:  IMAGE MAGICK S/W  (imagemagick) is required to install in server based on OS

Captcha in JSP and Servlet

Captcha in JSP and Servlet

This tutorial explains how to create Captcha in JSP and Servlet. This example is created in eclipse IDE and run on tomcat server.  This example using following jar

  • imaging.jar
  • jstl-1.2.jar
  • simplecaptcha-1.1.1.jar
  • standard.jar

The application directory structure will look like as below, if you are using an Eclipse editor for developing the application.
There are the following steps for creating a such application :.
Step 1 :
First we create Dynamic web project “captcha” in eclipse IDE . Again we create “simplecaptcha.jsp” file under WebContent folder. The code of  “simplecaptcha.jsp”  are given as:

<%@ taglib uri=; prefix=“c”%>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>



<META http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1”>

<title>Simple CAPTCHA Example</title>

<link href=“sc.css” type=“text/css” rel=“stylesheet” />




<h3>Simple CAPTCHA Example</h3>

<img id=“captcha” src=<c:url value=“simpleCaptcha.jpg” /> width=“150”>

<form action=“captchasubmit.jsp” method=“post”><input type=“text”

name=“answer” /><br>

<input type=“submit” value=“Submit”></form>




Step 2 :
Again we create “captchasubmit.jsp” file under WebContent folder. This file used displays message for captcha code are correct or not. The code of  “simplecaptcha.jsp”  are given as:

<%@ page import=“nl.captcha.Captcha”%>


Captcha captcha = (Captcha) session.getAttribute(Captcha.NAME);


String answer = request.getParameter(“answer”);

if (captcha.isCorrect(answer)) {


<center><b>Correct Captcha Code !</b> <%

else {

%> <b>In Correct Captcha Code !</b> <%




Step 3 :
 Now create css folder under WebContent  folder and create “styles.css” file under css folder. The code of  “styles.css” below :

body {



font-family“Helvetica Neue”, Arial, Helvetica, Geneva, sans-serif;


Step 4 :
Now you can open “web.xml” file and modify as:

<?xml version=“1.0” encoding=“UTF-8”?>

id=“WebApp_ID” version=“2.5”>














Step 5 :
Now download  imaging.jar, jstl-1.2.jar,simplecaptcha-1.1.1.jar and standard.jar  jar files and paste under  “WebContent/WEB-INF/lib” directory .
Step 6:
When run  this application on tomcat server the following output will displays as:
Again enter captcha code in text input box and click “Submit” button. If you enter invalid captcha code displays error message as :
If match captcha code displays message as:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: