Skip to main content

Creating captcha(random letter image) in jsp


A CAPTCHA is a program that can generate and grade tests that humans can pass but current computer programs cannot. For example, humans can read distorted text as the one shown below, but current computer programs can't:
The term CAPTCHA (for Completely Automated Public Turing Test To Tell Computers and Humans Apart) was coined in 2000 by Luis von Ahn, Manuel Blum, Nicholas Hopper and John Langford of Carnegie Mellon University. At the time, they developed the first CAPTCHA to be used by Yahoo.
recaptcha-example
Today we are going to learn how to create a random letter image known as captcha using servlet.
Just a simple coding can do it copy the code below and save it and compile it you can directly view the servlet in tomcat using url “http://localhost:8080/captcha/servlet/CaptchaServlet” for this make adjustment in web.xml file by adding servlet and servletmapping entries.


import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CaptchaServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

final int width = 200;
final int height = 50;

char data[][] = new char[1][];
data[0] = getRandomNumber(8).toCharArray();

final BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);

final Graphics2D g2d = bufferedImage.createGraphics();

final Font font = new Font("verdana", Font.BOLD, 18);

RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

renderingHints.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);

g2d.setRenderingHints(renderingHints);

g2d.setFont(font);
g2d.setColor(new Color(255, 255, 0));
final GradientPaint gradientPaint = new GradientPaint(0, 0, Color.white, 0, height / 2,
Color.white, true);

g2d.setPaint(gradientPaint);
g2d.fillRect(0, 0, width, height);

g2d.setColor(new Color(255, 155, 0));
Random random = new Random();

final String captcha = String.copyValueOf(data[0]);
request.getSession().setAttribute("captcha", captcha);

int xCordinate = 0;
int yCordinate = 0;

for (int i = 0; i < data[0].length; i++) {
xCordinate += 10 + (Math.abs(random.nextInt()) % 15);
if (xCordinate >= width - 5) {
xCordinate = 0;
}
yCordinate = 20 + Math.abs(random.nextInt()) % 20;
g2d.drawChars(data[0], i, 1, xCordinate, yCordinate);
}

g2d.dispose();

response.setContentType("image/png");
final OutputStream outputStream = response.getOutputStream();
ImageIO.write(bufferedImage, "png", outputStream);
outputStream.close();
}

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}

public static String getRandomNumber(int length) {

String chars = "www.s2sgateway.comabcdefghjijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ123456789";
Random random = new Random();

char[] buf = new char[length];

for (int i = 0; i < length; i++) {
buf[i] = chars.charAt(random.nextInt(chars.length()));
}

return new String(buf);

}

}





 

that's it you have created captcha image using servlet,Here you can give any letters or numbers in String chars = "";assignment values.
Change the color by changing this code "final GradientPaint gradientPaint = new GradientPaint(0, 0, Color.white, 0, height / 2,
Color.white, true);"Here i used this image as png because it is good for web development, you can try others as well like jpg or gif


Comments

Popular posts from this blog

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

Write a program to calculate the total expenses. Quantity and price per item are input by the user and discount of 10% is offered if the expense is more than 7000.

  #include<iostream.h> #include<conio.h> void main() { int totalexp, qty, price, discount; cout<<" Enter quantity: "; cin>>qty; cout<<" Enter price: "; cin>>price; totalexp=qty*price; if (totalexp>7000) { discount=(totalexp*0.1); totalexp=totalexp-discount; } cout<<" Total Expense is Rs. "<<totalexp; getch(); }