SimpleCaptcha is really nice and easy to use.
Here's an example how to use SimpleCaptcha with JSF 2.0 (the homepage has an example for JSP)
Note that I'm not even bothering to store the captcha value in the bean, I'm only validating it.
The bean:
// imports missing here
@ManagedBean
@SessionScoped
public class LoginBean implements Serializable
{
public void validateCaptcha(FacesContext context,
UIComponent componentToValidate,
Object value)
throws ValidatorException
{
HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
Captcha secretcaptcha = (Captcha) session.getAttribute(Captcha.NAME);
if (secretcaptcha.isCorrect(value.toString()))
return;
// optional: clear field
((HtmlInputText) componentToValidate).setSubmittedValue("");
throw new ValidatorException(new FacesMessage("Captcha does not match"));
}
}
The relevant segment of the facelet:
Type this:
The relevant segment of the web.xml:
SimpleCaptcha
nl.captcha.servlet.SimpleCaptchaServlet
captcha-width
250
captcha-height
75
SimpleCaptcha
/simpleCaptcha.png
Enjoy :-)