Java CAPTCHA Source Code principle

Source: Internet
Author: User
Tags rand

This article is divided into four steps:

First, the new servlet class named: Checkcodeservlet

Second, copy the following code into the new Checkcodeservlet class, modify your own package name


Package com.seesun2012.Servlet; //This is my package name.


Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
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 Checkcodeservlet extends HttpServlet {
Generate the font immediately
Private Font GetFont () {
Create a random object to generate a stochastic number
Random random = new random ();
Create a font array that is used to install font objects of different fonts
Font font[] = new FONT[5];
Font[0] = new Font ("Ravie", Font.plain, 24);
FONT[1] = new Font ("Antique Olive Compact", Font.plain, 24);
FONT[2] = new Font ("Forte", Font.plain, 24);
FONT[3] = new Font ("Wide Latin", Font.plain, 24);
FONT[4] = new Font ("Gill Sans Ultra Bold", Font.plain, 24);
Return Font[random.nextint (5)];
}

protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
Setting the response header Content-type type
Resp.setcontenttype ("Image/jpeg");
OutputStream OS = Resp.getoutputstream ();
int width =, height = 30;
Establishing a specified width, height, and BufferedImage object
BufferedImage image = new BufferedImage (width, height,bufferedimage.type_int_rgb);

Graphics g = image.getgraphics (); The brush is drawn on the image
Color C = G.getcolor (); Save the color of the current brush
Fill Rectangle
G.fillrect (0, 0, width, height);


char[] ch = "abcdefghjkmnpqrstuvwxyz2345678901". ToCharArray (); The resulting string does not include I l (lowercase l) o (lowercase o) 1 (number 1) 0 (number 0)
int length = Ch.length; The length of the string immediately
String SRand = ""; Save the resulting string
Random random = new random ();
for (int i = 0; i < 4; i++) {
Set font
G.setfont (GetFont ());
A 0-9 number is generated
String rand = new Character (ch[random.nextint (length)]). ToString ();
SRand + = rand;
Set Random Colors
G.setcolor (New Color (Random.nextint (255), Random.nextint (255), Random.nextint (255)));
g.DrawString (Rand, i + 6, 25);
}
Generate a random point of interference
for (int i = 0; i <; i++) {
int x1 = random.nextint (width);
int y1 = random.nextint (height);
G.drawoval (x1, y1, 2, 2);
}
Set the color of the brush back again
G.setcolor (c);
Releases the context of this graphic and all system resources it uses.
G.dispose ();

Record verification code to session
Req.getsession (). SetAttribute ("Safecode", SRand);
Output image to Page
Imageio.write (Image, "JPEG", OS);

}

protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
Doget (req, resp);
}

}



Third, add the following JavaScript code to the JSP page in the page (if you must refer to the Jquery-1.11.0.min.js script):

<script type= "Text/javascript" >
function changeimg () {
var imgobj = document.getElementById ("img");
IMGOBJ.SRC = "checkcodeservlet?ran=" + math.random ();
}
</script>



Iv. add in the body tag, post is the method of submission:

<form action= "to be processed and verify the servlet name" id= "Commentform" method= "POST" >

<span class= "Login_txt" > Verification Code:</span>

<input class= "" Name= "code" type= "Rext" value= "" Maxlength=4 size=10/>

<a href= "javascript:changeimg ()" class= "login-text03" > can't see Clearly, change picture </a>

</form>

Java CAPTCHA Source Code principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.