Wednesday , November 22 2017
Home ­­» Security ­­» Creating captcha application both online and offline using Java
captcha application

Creating captcha application both online and offline using Java

CAPTCHA (which stands for “Completely Automated Public Turing test to tell Computers and Humans Apart”) is a type of challenge-response test used in computing as an attempt to ensure that the response is generated by a person. e.g. website forms, by computer bots.

The use of CAPTCHA is normally intended to prevent spamming of sites or attempts to break into accounts. If your website needs protection from abuse, it is recommended that you use a CAPTCHA.

see also: How to connect USB Mouse and Keyboard to android device

There are many CAPTCHA implementations, some better than others.

We can implement captcha application in two modes:

  • Online reCAPTCHA.
  • Offline CAPTCHA.

Online reCAPTCHA

In this mode, we are going to use Google reCAPTCHA API.

What is reCAPTCHA?

reCAPTCHA is a free service from Google, which protects your website from spam and abuse. It uses an advanced risk analysis engine and adaptive CAPTCHAs to keep automated software from engaging in abusive activities on your site.

With the reCAPTCHA API, a significant number of your valid human users will pass the reCAPTCHA challenge without having to solve a CAPTCHA. reCAPTCHA comes in the form of a widget that you can easily add to your blog, forum, registration form, etc.

reCAPTCHA API available for Java, dotnet, PHP, WordPress and other languages.

Steps to implement Online reCAPTCHA :

  1. Open http://www.google.com/recaptcha this link in browser.
  2. Click on Get reCAPTCHA Button on the upper right corner.
  3. It will ask to login your Google account. Login to your Google account.
  4. If you don’t have Google account Click on “Sign up Now!” button and sign up to your gmail account in order to use reCAPTCHA.
  5. Once you login, click on Get reCAPTCHA Button on the upper right corner.
  6. You will get one form where you have to fill up the details, such as Label and Domains (can be anything,non-existing as well).  Check “Send alerts to owners” checkbox, so you can get alerts if Google detects problems with your site.

  7. Click on register. Now you will get Site key and Secret key. Note down these keys.
  8. We are now going to use those private and public keys in our Java application in order to Generate captcha.
  9. Download the latest re-Captcha library from the https://code.google.com/archive/p/recaptcha/downloads link. or download jar from here
  10. Create a New web Project in IDE (Eclipse / NetBeans). Include the jars into the project to show the image Include the following code in index.jsp

  11. index.jsp (text & number Captcha)

    Or you can use this index.jsp for different GUI

    reCAPTCHA

  12. index.jsp  (recaptcha with images and audio) Note: (Save it as index1.jsp, so you can check both demos)

     

  13. To Process the answer create process.jsp
  14. process.jsp

Online Demo of reCAPTCHA Click here

See Demo Tutorial

Download Eclipse Project (WAR)

see also: DataTables with Sorting, Searching and Paging

Offline Captcha:

We are going to use some other offline API’s. It generates captcha but it is one per session, so if you refresh the page,it wont give you new captcha.

Download jar here

1. Create a New web Project in IDE (Eclipse / NetBeans). Include the jars into the project to show the image Include the following code in index.jsp

index.jsp

2. To process the answer create process.jsp

process.jsp

3. Create web.xml

just change

with

to get different captcha.

Note: If we use SimpleCaptchaServlet in web.xml, will get new captcha image every-time when we refresh page.

 

About Rahul Mahadik

Rahul Mahdik is the Founder/Owner of TechnoScripts and also currently working with a leading web development company as a Software Developer. He received his Master’s degree in Computer Engineering from University of Pune. He is passionate about technical blogging, learn and explore new things and almost versatile in terms of programming across various languages & frameworks such as Java, Android, PHP, MySQL, WordPress,Bootstrap, HTML5, CSS3, Java Scripts, jQuery, Grails-Groovy and more.

Check Also

Data Table

Export HTML Table to Excel, Word, PDF, JSON, PNG, XML, CSV, TXT, and SQL using jQuery plugin

In every website and website admin panel, we come across through different types of reports …

11 comments

  1. Really Helpful article Sir.

  2. ” I want to use offline captcha. Which jars i needed, and from where should I download it. ”

    that should be cleared..

    • To use Offline captcha you required “simplecaptcha-1.2.1.jar”, search it on Google to download this file.
      ASAP I’ll provide direct link to download these libraries and war files so can directly test these examples.
      -Thank you

  3. Really Nice Article sir!

    I encountered problem with the page import at process.jsp for the Offline version sir.
    Err msg :
    Import cannot be resolved.
    and missing ReCaptchaImpl.

    Thank You

  4. Dear sir,
    i want to implement this code in java (core , JFrame).
    so please help.

    • Download and import recaptcha jar file in your project.

      Your index/home page code :
      Write one input submit tag and import java-script in FORM tag
      src=”http://api.recaptcha.net/challenge?k=YOUR_KEY”


      Class Code

      void index(){
      String remoteAddr = request.getRemoteAddr();
      ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
      reCaptcha.setPrivateKey(“YOUR_KEY”);

      String challenge = request.getParameter(“recaptcha_challenge_field”);
      String uresponse = request.getParameter(“recaptcha_response_field”);
      ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);

      if (reCaptchaResponse.isValid()) {
      println(“Answer was entered correctly!”);
      } else {
      println(“Answer is wrong”);
      }
      }

  5. hi sir ,
    I want to generate captcha in spring their we don’t have jsp pages and front-end framework is aurelia help me out to do.
    Thanks in advance.

    • Your Form:

      Start form tag
      start javsript and give this link to src
      src="http://api.recaptcha.net/challenge?k=YOUR_KEY"
      take sumbit button
      complle form tag

      Controller

      String remoteAddr = request.getRemoteAddr();
      ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
      reCaptcha.setPrivateKey("6LfFDCkTAAAAANMsbiRxBaOt4oycpLWpOZ3K-HTc");

      String challenge = request.getParameter("recaptcha_challenge_field");
      String uresponse = request.getParameter("recaptcha_response_field");
      ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);

      if (reCaptchaResponse.isValid()) {
      //Valid case
      }else{
      // invalid case
      }

  6. HTTP Status 500 – Internal Server Error Type Exception Report

    Message An exception occurred processing [/Process.jsp] at line [13]

    Description The server encountered an unexpected condition that prevented it from fulfilling the request.

    Exception
    org.apache.jasper.JasperException: An exception occurred processing [/Process.jsp] at line [13]

    10:
    11: String challenge = request.getParameter(“recaptcha_challenge_field”);
    12: String uresponse = request.getParameter(“recaptcha_response_field”);
    13: ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
    14:
    15: if (reCaptchaResponse.isValid())
    16: {

    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:593)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:482)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

    Root Cause
    java.lang.NullPointerException
    java.net.URLEncoder.encode(URLEncoder.java:204)
    java.net.URLEncoder.encode(URLEncoder.java:170)
    net.tanesha.recaptcha.ReCaptchaImpl.checkAnswer(ReCaptchaImpl.java:58)
    org.apache.jsp.Process_jsp._jspService(Process_jsp.java:126)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet
    please help me sir this is show error message.

Leave a Reply

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

Pin It on Pinterest