JavaServer Faces Validator CAPTCHA Code Example
First Time Here?
Check the BotDetect JSP Captcha Quickstart for key integration steps.
The JSF Validator Captcha code example shows how to integrate BotDetect CAPTCHA validation with standard JavaServer Faces page validation functionality and other validator controls.
Downloaded Location
The JSF Validator CAPTCHA Example is included in examples
folder of the download package as bdc3-jsf-validator-captcha-example.war
file. Deploying (unpacking) the file will create a standard JSP directory tree.
- index.jsp
- WEB-INF/src/botdetect/examples/jsf/validator/ExampleValidator.java
- WEB-INF/src/botdetect/examples/jsf/validator/view/ValidatorExample.java
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <%@taglib prefix="botDetect" uri="botDetect"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <f:view> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>BotDetect JSF Validator CAPTCHA Example</title> <link rel="stylesheet" href="stylesheet.css" type="text/css"/> </head> <body> <h:form prependId="false"> <h1>BotDetect JSF Validator CAPTCHA Example</h1> <fieldset> <legend>CAPTCHA Validation</legend> <h:outputLabel for="inputName" value="Name:"/> <br/> <h:inputText id="inputName" validatorMessage="Missing name!"> <f:validateLength minimum="2"/> </h:inputText> <h:message for="inputName" styleClass="incorrect"/> <br/> <h:outputLabel for="captchaCodeTextBox" value="Retype the code from the picture:"/> <!-- Adding BotDetect Captcha to the page --> <botDetect:jsfCaptcha id="exampleCaptcha"/> <div class="validationDiv"> <h:inputText id="captchaCodeTextBox" value="#{validatorExample.captchaCode}" validatorMessage="Incorrect CAPTCHA code!"> <f:validator validatorId="exampleValidator"/> <f:attribute name="captchaId" value="exampleCaptcha"/> </h:inputText> </div> <h:commandButton action="#{validatorExample.submit}" value="Submit"/> <br/> <h:message for="captchaCodeTextBox" styleClass="incorrect"/> <h:outputText value="Validation passed." styleClass="correct" rendered="#{validatorExample.validated}"/> </fieldset> </h:form> </body> </html> </f:view>
In order to use a custom JSF Validator to validate user Captcha code input, we have to declare a validator of the Captcha code text box (<f:validator validatorId="exampleValidator"/>
). Also, we have to add Captcha Id as component's attribute to Captcha code text box (<f:attribute name="captchaId" value="exampleCaptcha"/>
).
ExampleValidator.java
package botdetect.examples.jsf.validator; import botdetect.web.Captcha; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.FacesValidator; import javax.faces.validator.Validator; import javax.faces.validator.ValidatorException; import javax.servlet.http.HttpServletRequest; @FacesValidator(value="exampleValidator") public class ExampleValidator implements Validator{ public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { String captchaId = (String)component.getAttributes().get("captchaId"); HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest(); // validate the Captcha to check we're not dealing with a bot Captcha captcha = Captcha.load(request, captchaId); boolean isHuman = captcha.validate(request, (String)value); if (!isHuman) { FacesMessage message = new FacesMessage(); throw new ValidatorException(message); } } }
In validaton method of the JSF custom validator first we have to obtain Captcha id from component's attributes.
Using Captcha id instantiate Captcha
object and validate user input in usual way.
ValidatorExample.java
package botdetect.examples.jsf.validator.view; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean(name="validatorExample") @RequestScoped public class ValidatorExample { private String captchaCode; private boolean validated; public ValidatorExample() { } public String getCaptchaCode() { return captchaCode; } public void setCaptchaCode(String captchaCode) { this.captchaCode = captchaCode; } public boolean isValidated() { return validated; } public void submit(){ captchaCode = null; validated = true; } }
Managed bean has no specific BotDetect Captcha code since all validation is done in custom validator. Form's action method submit()
is invoked after Captcha validation is passed so property validated
(which is used to display message on the form) is set true
.
To run this example, botdetect.jar
must be in the classpath.
Please Note
BotDetect Java Captcha Library v4.0.Beta3.7 is an in-progress port of BotDetect 4 Captcha, and we need you to guide our efforts towards a polished product. Please let us know if you encounter any bugs, implementation issues, or a usage scenario you would like to discuss.
Current BotDetect Versions
-
BotDetect ASP.NET CAPTCHA
2019-07-22v4.4.2 -
BotDetect Java CAPTCHA
2019-07-22v4.0.Beta3.7 -
BotDetect PHP CAPTCHA
2019-07-22v4.2.5