JSP Verification Code Encyclopedia of Digital Verification Code

Source: Internet
Author: User
Tags rand


Verification code applications in a variety of occasions, very wide, in a variety of systems or software in the new user account registration, user login, Web site unified Landing or users in the website published information module published articles or content are added to the random code function, the user's operating process to carry out a verification, The purpose of using Authenticode is to avoid misuse of autoenrollment programs or automatic publishing programs on the network. The principle of verification code is to randomly select some character code to display the character code in the form of a picture in the software verification interface or verification page. When the user needs to enter the validation code on the picture and submit it at the same time as the commit, if the character verification code is the same as the character code saved by the server session, If the submission is considered valid, the submission is refused. In the process of using the verification code, in order to avoid the automatic analysis program to parse the picture and obtain the verification information, usually need to randomly generate some interference lines in the picture or the complex characters to distort the image blurred, thereby increasing the automatic recognition program to analyze the difficulty of verifying the picture.



JSP authentication Code Source encyclopedia will be used in several pages of content to introduce in the JSP several authentication code to achieve the source code and use.



First, the JSP generated digital authentication code source



Digital verification Code is one of the most commonly used authentication character code form, the following is a digital implementation of JSP source code:


num.jsp
<%@ page contenttype= "image/jpeg" import= "java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%>
<%!
Color getrandcolor (int cc,int BB)
{
Random Random = new Random ();
if (fc>255) cc=255;
if (bc>255) bb=255;
int R=cc+random.nextint (BB-CC);
int G=cc+random.nextint (BB-CC);
int B=cc+random.nextint (BB-CC);
Returnnew Color (R,G,B);
}//Get random color
%>
<%
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);
int width=80; Define the length of the Verification code picture
int height=30; Define the width of the verification code picture
BufferedImage image = new BufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics g = image.getgraphics ();
Random Random = new Random ();
G.setcolor (Getrandcolor (200,250));
G.fillrect (0, 0, width, height);
G.setfont (New Font ("Times New Roman", font.plain,18));
Define font Form
G.setcolor (Getrandcolor (160,200));
for (int i=0;i<155;i++)
{
int i_x = random.nextint (width);
int i_y = random.nextint (height);
int I_XL = Random.nextint (12);
int i_yl = Random.nextint (12);
G.drawline (I_x,i_y,i_x+i_xl,i_y+i_yl);
}
Draw a background with a line
String s_rand= "";
for (int i=0;i<4;i++)
{
String rand=string.valueof (Random.nextint (10));
S_rand+=rand;
G.setcolor (New Color (20+random.nextint), 20+random.nextint (a), 20+random.nextint (110));
g.DrawString (rand,13*i+6,16);
}
Generates 4-bit random code
Session.setattribute ("Rand", S_rand);
To deposit a CAPTCHA in session
G.dispose ();
Imageio.write (Image, "JPEG", Response.getoutputstream ());
Output validation Picture
Out.clear ();
out = Pagecontext.pushbody ();
%>


The next article will introduce JSP in the Chinese authentication code, other Java verification code implementation, as well as the JSP in the code of the call and use of relevant content, the next article will introduce the JSP Chinese verification code to achieve the source code.


Related Article

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.