How to configure JCaptcha with your Spring App

Update: 03/17/2009: This code example uses JCaptcha 1.0-RC6 . The latest version released on 02/04/2009 is JCaptcha 1.0. If you use JCaptcha 1.0 then you might have to make some changes to the code given in this example.

This is how I setup JCaptcha in my application. Following code snippet is for the test page I have created to test the JCaptcha and Spring integration


1) First create a controller which will generate a new Captcha for each session based on session id

Please note that your implementation can vary. The only thing you want is a simple Servlet which will execute the code in showForm method above. In my case I am relying on Spring framework to execute this code because in the JSP where you show Captcha image I have written something like this
<img id=”captchaImage” src=”http://localhost:8080/LoginApplication/captcha.htm”>. In my application the URL captcha.htm is wired to this controller. But you can choose to have a simple servlet mapped instead of a Spring Controller.

2) Make the following entries in your Spring config files to configure JCaptcha

For test purpose I have used a Simple Text Captcha.

3) To test create a command,controller and JSP like this

As you can see the JSP will just show one Captcha Image and a text box where you can enter the Image text. The controller will verify the entered text and display true or false based on the result. You can implement your logic here for the Captcha verification result.

4) Finally make the following entries in your corresponding spring config file

Oops I forgot to mention, you should have JCaptcha jars in your classpath for this example to work πŸ˜‰
I have written this post using the working code I have. I might have missed some configuration in this post. Let me know if you get any error. I will update the post.

5 thoughts on “How to configure JCaptcha with your Spring App

  1. Alice

    Hi Paras,

    Your tutorial is great, I could make with it Captcha in Spring inspite of that I am new in that area!

    But I have an error.

    At me happens, what shouldn’t happen πŸ™‚ : The Catcha servlet
    get an other sessionId as my “main” servlet, and so the response is not correct.

    I think I haven’t configured something and that’s why don’t work it.

    Could you help me, what’s my error?

    Thank you in anticipation


    I attach the relevant sources form my project and a logfile.

  2. Alice

    Hello Paras, Hello Everebody,

    Thank you very much for your incredibly rush and effective help. It was very kind of you!

    Paras responses me per mail in a half an hour and hereby I could idenetify my problem.

    The symptom is the following:

    The sessionId problem is occurs just if I use stand alone Tomcat (Vers 6.0)!!

    If I run my project under Eclipse plugged Tomcat the two session (and the ID) is identical, and the response controll is Ok.

    I suppuse that is a configuration problem in my project but I don’t know what.

    If you , or somebody else has a suggestion it would be nice!

    Thank you in anticipation


  3. hadi

    Hi to all πŸ™
    i cant run this project. when i run it appear this error HTTP Status 404 – /JCapatcha/
    please help me.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.