Sunday, August 4, 2013

Implementing Captcha for J2EE application


Implementing Captcha for J2EE application

Here I am going to explain how you can add captcha to J2EE based web application.

List of jar files requored:
1.            jcaptcha-2.0-alpha-1-SNAPSHOT.jar
2.            jcaptcha-api-1.0.jar
3.            jcaptcha-integration-simple-servlet-2.0-alpha-1-SNAPSHOT.jar
As you going to have captcha for you application you have to make below entry in you web.xml file.

Servlet definition for Captcha

<servlet>
        <servlet-name>jcaptcha</servlet-name>
        <servlet-class>com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet</servlet-class>
  </servlet>

Servlet mapping for captcha

<servlet-mapping>
        <servlet-name>jcaptcha</servlet-name>
        <url-pattern>/jcaptcha.jpg</url-pattern>
  </servlet-mapping>

 

Show captcha Image

<p:graphicImage id="captchaImage" value="jcaptcha.jpg" />

 

Taking input from user

<p:inputText styleClass="textfield"  value="#{customerBean.captchaText}" required="true" requiredMessage="Enter security code"></p:inputText>

 

Validating Input with captcha text

HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
                        boolean isCaptchaValidate = SimpleImageCaptchaServlet.validateResponse(request,customerBean.getCaptchaText());
                       


Now you can enjoy captcha on you applicaiton