MintEye gives Captcha Java API for Web Application

Everytime when we make a web form or contact form then we use captcha for authenticate the real user exist or not.
But are you bored to use old fashioned captcha. Then this time MintEye give you Captcha Java API which give you a really cool and unique captcha.

MintEye Java Library

To use minteye with Java, download minteye Java library, just to make things easier for you.
Unzip and put the adscaptcha.jar in the classpath of your web application.


Recommended article : How to add Captcha in Java Using Google new reCaptcha


For example, if you are using Tomcat to run JSP, you may copy the jar file to the WEB-INF/lib/ directory.

In order to use the Java library, you'll need to import the minteye classes. In JSP, add this line in the page where the minteye will be displayed:
<@ page import="net.adscaptcha.AdsCaptchaAPI">

CLIENT SIDE - DISPLAY YOUR CAPTCHA

Now you're ready to display your minteye.
Place this code inside your <form> where the minteye will be placed:
final String captchaId  = "your_captcha_id";
final String publicKey  = "your_public_key";
AdsCaptchaAPI adscaptcha = new AdsCaptchaAPI();
out.print(adscaptcha.getCaptcha(captchaId, publicKey));
Don't forget to set your Captcha ID and Public Key values.

Check Demo Here

SERVER SIDE - VALIDATE YOUR CAPTCHA

On your validation process, place this code:
final String captchaId  = "your_captcha_id";
final String privateKey = "your_private_key";
final String challengeValue = request.getParameter("adscaptcha_challenge_field");
final String responseValue = request.getParameter("adscaptcha_response_field"); final String remoteAddress = request.getRemoteAddr();
AdsCaptchaAPI adscaptcha = new AdsCaptchaAPI();
String result = adscaptcha.validateCaptcha(captchaId, privateKey, challengeValue, responseValue, remoteAddress);
if (result.equalsIgnoreCase("true"))
{
// Corrent answer, continue with your submission process
}
else
{
// Wrong answer, you may display a new minteye and add an error args
}
Don't forget to set your Captcha ID and Private Key values.

Download Project Here

                                
                                

How to Use Quartz Scheduler with Java Web Application

In our previous article we learn about Quartz 2 hello world example along with types of Triggers in Quartz 2, In this article we shall learn to Integrating Quartz 2 Scheduler in Java Web Application.



Quartz, is an open source job scheduling framework, that let you schedule a task to run on a predefine date and time.

Library required


  • commons-logging-1.1.1.jar
  • log4j-1.2.16.jar
  • quartz-2.2.1.jar
  • slf4j-api-1.6.6.jar
  • slf4j-log4j12-1.6.6.jar
You should knowReason behind the naming of Great Project

Scheduler Job

Create a Quartz’s job
package com.blogspot.geekonjava.quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class QuartzJob implements Job {
        public void execute(JobExecutionContext context)
                        throws JobExecutionException {
                System.out.println("Java web application + Quartz 2.2.1");
        }
}
 Read also :Why Quartz doesn't remove deleted job from the scheduling

Servlet Listener

Now Create servlet listener class by implementing ServletContextListener interface and override contextInitialized and contextDestroyed methods with your logic’s.
contextInitialized() method will be executed automatically during Servlet container initialization, which in turn calls the Quartz scheduler job, which gets executed every 10 seconds.
contextDestroyed() method will be executed when the application shuts down, So in this function I have invoked the shutdown function of quartz scheduler
package  com.blogspot.geekonjava.quartz;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzListener implements ServletContextListener {
        Scheduler scheduler = null;
        @Override
        public void contextInitialized(ServletContextEvent servletContext) {
                System.out.println("Context Initialized");
               
                try {
                        // Setup the Job class and the Job group
                        JobDetail job = newJob(QuartzJob.class).withIdentity(
                                        "CronQuartzJob", "Group").build();
                        // Create a Trigger that fires every 5 minutes.
                        Trigger trigger = newTrigger()
                        .withIdentity("TriggerName", "Group")
                        .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                        .build();
                        // Setup the Job and Trigger with Scheduler & schedule jobs
                        scheduler = new StdSchedulerFactory().getScheduler();
                        scheduler.start();
                        scheduler.scheduleJob(job, trigger);
                }
                catch (SchedulerException e) {
                        e.printStackTrace();
                }
        }
        @Override
        public void contextDestroyed(ServletContextEvent servletContext) {
                System.out.println("Context Destroyed");
                try
                {
                        scheduler.shutdown();
                }
                catch (SchedulerException e)
                {
                        e.printStackTrace();
                }
        }
}

web.xml

Configure the listener class QuartzSchedulerListener.java into the web.xml file as shown below
<listener>
<listener-class>com.quartz.QuartzListener</listener-class>
</listener>

How to Scheduling Job in Java via Quartz Scheduler

Quartz scheduler to help Java application to scheduler a job/task to run at a specified date and time.

Library


  • commons-logging-1.1.1.jar
  • log4j-1.2.16.jar
  • quartz-2.2.1.jar
  • slf4j-api-1.6.6.jar
  • slf4j-log4j12-1.6.6.jar

Quartz Job

Quartz job is defined what you want to run


package com.blogspot.geekonjava.quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
public class QuartzJob implements Job {
        public void execute(JobExecutionContext context)
                        throws JobExecutionException {
                JobKey jobKey = context.getJobDetail().getKey();
                System.out.println("Quartz " + "Job Key " + jobKey);
        }
}

Quartz Trigger

Quartz trigger is defined when the Quartz will run your above Quartz’s job

Note : Why Quartz doesn't remove deleted job from the scheduling

There are two types of triggers in Quartz 2

  1. SimpleTrigger – Allows to set start time, end time, repeat interval.
  2. CronTrigger – Allows Unix cron expression to specify the dates and times to run your job.

SimpleTrigger – Run every 10 seconds.

Trigger trigger = newTrigger().withIdentity("TriggerName", "Group1")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(10).repeatForever()).build();

CronTrigger – Run every 10 seconds

Trigger trigger = newTrigger()
                .withIdentity("TriggerName", "Group2")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();

Scheduler

Scheduler class links both “Job” and “Trigger” together and execute it.
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);

Full Example

Quartz 2 full examples with SimpleTrigger and CronTrigger.
 package com.blogspot.geekonjava.quartz;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzSchedulerDemo {
public static void SimpleTriggerExample() throws SchedulerException {
        JobDetail job = newJob(QuartzJob.class).withIdentity(
                        "SimpleQuartzTrigger", "Group1").build();
        Trigger trigger = newTrigger()
                        .withIdentity("TriggerName", "Group1")
                        .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(10).repeatForever())
                        .build();
        Scheduler sched = new StdSchedulerFactory().getScheduler();
        sched.scheduleJob(job, trigger);
        sched.start();
}
public static void CronTriggerExample() throws SchedulerException {
        JobDetail job = newJob(QuartzJob.class).withIdentity(
                        "CronQuartzTrigger", "Group2").build();
        Trigger trigger = newTrigger()
                        .withIdentity("TriggerName", "Group2")
                        .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                        .build();
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);
}
public static void main(String[] args) throws SchedulerException {
        SimpleTriggerExample();
        CronTriggerExample();
}
}