Simple Servlet Integration documentation

Description

This integration module provides the shortest way to integrate JCaptcha to your web application.
This module is available since jcaptcha 2.0

This integration module provides the new default captcha configuration :

All you need to do is add the jcaptcha jar to your project, make a reference to the SimpleImageCaptchaServlet in your web.xml and then use the servlet session id to validate what the user submits on your form against the captcha service.

Set up

  • Add the jcaptcha jar file into the WEB-INF/lib directory of your .war file
  • If your app or server do not already have commons-logging and commons-collection into the WEB-INF/lib directory of your .war
  • Put the reference in your web.xml (checking that the url-pattern path matches up with what you put in your html fragment above):
<servlet>
        <servlet-name>jcaptcha</servlet-name>
        <servlet-class>com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>jcaptcha</servlet-name>
        <url-pattern>/jcaptcha.jpg</url-pattern>
</servlet-mapping>
  • Add the folowing image tag to the form you want to protect, this call will asks the SimpleImageCaptchaServlet to generate a fresh new captcha
<form action="submit.action">
...
    <img src="jcaptcha.jpg" /> <input type="text" name="jcaptcha" value="" />
...
</form>
  • In your code that manages the submit action add the following code fragment to validate the user input
String userCaptchaResponse = request.getParameter("jcaptcha");
boolean captchaPassed = SimpleImageCaptchaServlet.validateResponse(request, userCaptchaResponse);
if(captchaPassed){
// proceed to submit action
}else{
// return error to user
}

And that's it!

Sample Web App

Jcaptcha 2.0 jars