Friday 10 July 2015

How to Use CAPTCHA in ADF application

This blog we will see, how to use CAPTCHA in ADF application.
CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) is a challenge-response system test designed to differentiate humans from automated programs. The sample of CAPTCHA is like below                                                

   We need to follow the below steps for enabling CAPCHA in ADF application
    1. Create a fusion middleware web application. 
    2.  Download the simplecaptcha-1.2.1.jar. This jar file you can download from internet. 
    3.  Add the same jar file into the project. Now right click on the viewcontroller project and select project properties. Then select “Library and Classpath” and click on “Add JAR/Directory…” and the jar file.

            4.       Add the following code in web.xml file.



      5.       Add the following code in your page(.JSPX/.jsff)

<af:panelFormLayout id="pfl1">
          <f:facet name="footer"/>
          <af:outputText value="Captcha Demo" inlineStyle="font-size:22pt;"/>
          <af:image source="/captchaservlet" id="i1"
                    inlineStyle="width:200px; height:60.0px;"/>
          <af:commandButton text="Refresh" id="cb2"
             <af:panelLabelAndMessage label="Enter the Text : " id="plam1">
            <af:panelGroupLayout id="pgl1" layout="horizontal" halign="left">
              <af:inputText id="it1" value="#{requestScope.enterTxt}"/>
              <af:commandButton text="try" id="cb1"
                                partialSubmit="true" immediate="false"/>
            <af:message id="m1" for="it1"/>
      6.       Add the below code into your bean.

    public void verifyAnswer(ActionEvent actionEvent) {
        // Add event code here...
        FacesContext fctx = FacesContext.getCurrentInstance();
        ExternalContext ectx = fctx.getExternalContext();
        HttpServletRequest request = (HttpServletRequest)ectx.getRequest();
        Captcha captcha = (Captcha)ectx.getSessionMap().get(Captcha.NAME);
        try {
        } catch (UnsupportedEncodingException e) {
            System.out.println("UTF not supported !");
        String answer = (String)ectx.getRequestMap().get("enterTxt");
        if (answer != null && captcha.isCorrect(answer)) {
            message("You are correct boss.");
        } else {
            message("You are wrong.Please try again.");
            UIComponent panelLabelAndMessage =
            UIComponent panelFormlayout = panelLabelAndMessage.getParent();


    private void message(String str) {
        FacesContext fctx = FacesContext.getCurrentInstance();
        fctx.addMessage("it1", new FacesMessage(str));
      7.       Now run your application. It will display like the below.

You Can Download the code: Download


1 comment:

  1. which version of ADF is supported please? What happen if the CAPCHA image cannot display?