CAPTCHA with Seam in Three Minutes

Home/Seam, Security, Spam/CAPTCHA with Seam in Three Minutes

Adding a CAPTCHA to a form using Seam is easy now that Seam is bundling jCaptcha.

The Seam documentaiton is good, and can be found in section 13.9 here:

If you used seam-gen to create your project, you will need to make a few changes.

First, you need to modify your project’s ant build script to deploy the captcha jar into your ear (or possibly .war). In the target “ear” of the build.xml file, you will find a list of many jar files being copied from your project’s lib directory into the ear. Simply add the captcha jar to that list, like this:

<include name="lib/jcaptcha-all-1.0-RC6.jar"></include>

Now that the jar is deploying, you need to reference it in the application.xml file found under your project’s resources/META-INF directory. Add this entry:


If you used seam-gen you will find that the Seam Resource Servlet is already defined in your web.xml so the step defined in the documentation in section 13.9.1 is not necessary.

Now all you have to do is to add the tags into your page to firstly display the captcha image, and secondly take user input and validate the captcha. The validation process is handled out of the box by the Seam captcha component, so you do not need to handle that yourself. The documentation has this as an example captcha usage to paste into your page:

<h:graphicImage value="/seam/resource/captcha?#{}"/>

<h:outputLabel for="verifyCaptcha">Enter the above letters</h:outputLabel>
<h:inputText id="verifyCaptcha" value="#{captcha.response}" required="true">
<s:validate />
<div class="validationError"><h:message for="verifyCaptcha"/></div>

<h:commandButton action="#{}" value="Register"/>

I am using icefaces in my latest project, so I added it like this:

<h:outputLabel for="captchaImage"> Captcha <span class="required">*</span> </h:outputLabel>

<h:graphicImage value="/seam/resource/captcha?#{}" />

<h:outputLabel for="verifyCaptcha"> Enter the above letters * </h:outputLabel>
<ice:inputText id="verifyCaptcha" partialSubmit="false" required="true" size="45" maxlength="45"

Once you deploy your project and start your application server, your page will look something like this:

Captcha registration page

And that is it. Very simple and easy.

Personally I find the default jCaptcha images to be very difficult to read (I picked an easy one above), so I will probably end up implementing a custom configured captcha using the directions in section 13.9.3 of the documentation. When that happens I will make it available here.

By | 2017-05-18T15:18:39+00:00 October 21st, 2007|Seam, Security, Spam|1 Comment

About the Author:

One Comment

  1. hiran April 15, 2009 at 9:47 am - Reply

    ty man!

    helped a lot =]

Leave A Comment