PrimeFaces Captcha Example

Captcha (Completely Automated Public Turing test to tell Computers and Humans Apart)  is a type of challenge response test used in computing to determine whether or not the user is a human. PrimeFaces had provided such a component when the first version released. The component tag is <p:captcha/>.

Captcha is implemented as an input component with a built-in validator that is integrated with reCaptcha. reCAPTCHA is a free service to protect your website from spam and abuse provided by Google.

1. Captcha Tag Info

Captcha Tag Info

2. Captcha Tag Attributes

Captcha Tag Attributes

Captcha Attributes 2

3. Getting Public & Private Key

For getting the public and private key for be able of using Primefaces Captcha component, you have to visit the following link (After login) and follow the below remaining steps:

  • Press Get reCAPTCHA.
  • Press Sign Up Now !.
  • Put your domain inside the input box that follow word Domain.
  • Press Create.
  • Copy Public Key Private Key that have provided dynamically.

4. Configure the web.xml

By copying the public and private keys that listed above, you have to configure the web.xml file, in order for getting working Captcha. In case, you’ve omitted the mentioning of such those keys, you will be getting an exception like below:

Apr 16, 2014 4:09:58 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/JavaBeat-Primefaces-V4] threw exception [Cannot find public key for catpcha, use primefaces.PUBLIC_CAPTCHA_KEY context-param to define one] with root cause
javax.faces.FacesException: Cannot find public key for catpcha, use primefaces.PUBLIC_CAPTCHA_KEY context-param to define one

The snippet of code that would be adding for the web.xml is as follow:



5. Managed Bean

package net.javabeat.primefaces;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

public class Captcha {
	public void check(ActionEvent e){
				new FacesMessage(FacesMessage.SEVERITY_INFO, "Your Code Is Correct !",null));

6.The View


<html xmlns=""
		<script name="jquery/jquery.js" library="primefaces"></script>
		<h:form prependId="false">
			<h1>JavaBeat Primefaces Example</h1>
			<h2>Primefaces - Captcha</h2>
			<p:messages id="msg"/>
			<p:captcha id="captcha"></p:captcha>
			<p:commandButton value="Check" actionListener="#{captcha.check}"

7. Primefaces Captcha Example Demo

The below snapshots shows you the using of captcha and how could the success code

Primefaces Captcha Example Demo

Primefaces Captcha Example 2

Primefaces Captcha Example 3

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!