Joke Collection Website - Blessing messages - How to realize verification code with java
How to realize verification code with java
The verification code is actually to randomly select some characters and display them on the page in the form of pictures. If the characters on the picture need to be submitted at the same time, if the submitted characters are different from those stored in the server session, the submitted information is considered invalid. In order to avoid the automatic analysis of pictures by programs, some interference lines or distorted characters are usually randomly generated on pictures to increase the difficulty of automatic identification of verification codes.
Here we use java to realize the verification code.
& lt% @ page content type = image/JPEG import = Java awt * Java awt image * Java util * javax imageio * % & gt;
& lt%!
Color get rand color (int fc int BC) {//Get random colors in a given range.
Random Random = new Random();
if(fc & gt; )fc =;
if(BC & gt; )BC =;
int r = fc+random nextInt(BC fc);
int g = fc+random nextInt(BC fc);
int b = fc+random nextInt(BC fc);
Return the new color (r gb);
}
% & gt
& lt%
//Set the page not to be cached.
response set header(Pragma No cache);
Response setHeader (cache control without cache);
Response setDateHeader (expired);
//Create an image in memory
int width = height =
buffered image image = new buffered image(width height buffered image TYPE _ INT _ RGB);
//Get the graphic context
graphics g = image get graphics();
//Generate a random class
Random Random = new Random();
//Set the background color
g set color(getRandColor());
G fillRect (width and height);
//Set the font
G setFont (Times New Roman Font PLAIN));;
//Random interference lines make the authentication code in the image difficult to be detected by other programs.
g set color(getRandColor());
for(int I =; I <; i++)
{
int x = random nextInt(width);
int y = random nextInt(height);
int XL = random nextInt();
int yl = random nextInt();
g drawLine(x y x+XL y+yl);
}
//Take the randomly generated authentication code (number)
string codeList = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz;
string sRand =;
for(int I =; I <; i++){
int a = random nextInt(codeList length());
string rand = codeList substring(a a+);
sRand+= rand;
//Display the verification code in the image.
g set Color(new Color(+random nextInt()+random nextInt()+random nextInt()); //The colors generated by calling the function are the same, probably because the seeds are too close, so they can only be generated directly.
G draw rope (rand * I+);
}
//Store the authentication code in the session.
session set attribute(rand sRand);
//Image takes effect
g dispose();
//Output the image to the page
ImageIO write(image JPEG response get output stream());
out clear();
out = pageContext push body();
Lishi Xinzhi/Article/program/Java/hx/20 13 1 1/25536
- Related articles
- Can mobile phones only block short messages? But mobile phones can still receive mobile phones. Can you directly block short messages?
- Is there a charge for adding SMS inquiry service to Longka Credit Card?
- What is the message "Untitled, Failed"?
- How to calculate the mobile phone traffic in Xiangtan, Hunan? How much m traffic does a 5-yuan GPRS package have per month?
- Does China Postal Savings Bank open SMS to remind consumers?
- How can I find out which star I belong to?
- I am a security guard. I asked for leave from the security company because of the epidemic. Is it reasonable for the company to let me leave?
- How to use Fetion mobile phone? Is it free to send short messages by Fetion?
- The zone transfer problem in Fantasy Westward Journey, experts come in! .....100 points
- Basic principles of contract performance