JSP production verification code

Source: Internet
Author: User
I just got a JSP verification code from yuanshao. Hey, I tested it myself. At the beginning, it was always unsuccessful and there was always an exception. Then I found out that I wrote println as pritnln. Alas, later, we have to combine JB and DW to write JSP ....
The following is the source code of the JSP verification code, and the following is the digital verification code generated:

<% @ Page contenttype = "image/JPEG" Import = "Java. AWT. *, Java. AWT. image. *, Java. util. *, javax. imageIO. * "%>
<%!
Color getrandcolor (int fc, int BC) {// obtain a random color from a given range
Random random = new random ();
If (FC> 255) fc = 255;
If (BC> 255) BC = 255;
Int r = FC + random. nextint (BC-Fc );
Int G = FC + random. nextint (BC-Fc );
Int B = fc + random. nextInt (bc-fc );
Return new Color (r, g, B );
}
%>
<%
// Set the page not to cache
Response. setHeader ("Pragma", "No-cache ");
Response. setHeader ("Cache-Control", "no-cache ");
Response. setDateHeader ("Expires", 0 );

// Create an image in memory
Int width = 60, height = 20;
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );

// Obtain the image Context
Graphics g = image. getGraphics ();

// Generate a random class
Random random = new Random ();

// Set the background color
G. setColor (getRandColor (200,250 ));
G. fillRect (0, 0, width, height );

// Set the font
G. setFont (new Font ("Times New Roman", Font. PLAIN, 18 ));

// Draw a border
// G. setColor (new Color ());
// G. drawRect (0, 0, width-1, height-1 );

// Generates 155 random interference lines, making the authentication code in the image hard to be detected by other programs.
G. setColor (getRandColor (160,200 ));
For (int I = 0; I <155; I ++)
{
Int x = random. nextInt (width );
Int y = random. nextInt (height );
Int xl = random. nextInt (12 );
Int yl = random. nextInt (12 );
G. drawLine (x, y, x + xl, y + yl );
}

// Obtain the random ID code (4 digits)
String sRand = "";
For (int I = 0; I <4; I ++ ){
String rand = String. valueOf (random. nextInt (10 ));
SRand + = rand;
// Display the authentication code to the image
G. setColor (new Color (20 + random. nextInt (110), 20 + random. nextInt (110), 20 + random. nextInt (110); // The color from the call function is the same, probably because the seed is too close, so it can only be generated directly.
G. drawString (rand, 13 * I + 6, 16 );
}

// Save the authentication code to the SESSION
Session. setAttribute ("numrand", sRand );

// The image takes effect
G. dispose ();

// Output the image to the page
ImageIO. write (image, "JPEG", response. getOutputStream ());
Out. clear ();
Out = pageContext. pushBody ();
%>

There is also a source code. The following source code generates a Chinese verification code:

<% @ Page contentType = "image/jpeg" import = "java. awt. *, java. awt. image. *, java. util. *, javax. imageio. * "%>
<%!
// Create by smallnest
// Email: smallnest@gmail.com
// Website: www.kuaff.com

// Generate random colors
Color getRandColor (Random random, int fc, int bc)
{
If (fc> 255) fc = 255;
If (bc> 255) bc = 255;
Int r = fc + random. nextInt (bc-fc );
Int g = fc + random. nextInt (bc-fc );
Int B = fc + random. nextInt (bc-fc );
Return new color (R, G, B );
}
%>
<%
// Set the page not to cache
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );

// Set the length and width of the image
Int width = 176, Height = 30;
// Set Alternative Chinese characters to remove some indecent Chinese Characters
String base = "/u7684/u4e00/u4e86/u662f/u6211/latest/u5728/latest/u6765/latest/u8fd9/latest/u7740/u4e2a/u5730/u5230 /u5927/u91cc/u8bf4/examples/u53bb/examples/u90a3/u8981/examples/u65f6/u8fc7/u51fa/u5c0f/u4e48/u8d77 /users/u90fd/u628a/u597d/u8fd8/u591a/u6ca1/u4e3a/u53c8/u53ef/u5bb6/users/u53ea/users/u6837/users/u60f3/users /users/u8001/users/u4ece/u81ea/u9762/u524d/u5934/u9053/u5b83/u540e/users/u50cf/u89c1/users/u7528/u5979 /u56fd/u52a8/u8fdb/u6210/u56de/examples/u800c/examples/u5c71/u6c11/u5019/u7ecf/u53d1/u5de5 /u5411/u4e8b/u547d/u7ed9/latest/u4e09/u58f0/latest/u773c/u5fd7/u70b9/u5fc3/u6218 /users/u95ee/u4f46/u8eab/u65b9/users/u5403/u505a/u53eb/users/u542c/u9769/users/u5462/users/u5168/users/u56db/users /u6240/latest/u6700/u5149/u4ea7/u60c5/u8def/u5206/u603b/u6761/u767d/latest/u5982/u88ab/u82b1 /u53e3/u653e/u513f/u5e38/queues/u7b2c/queues/u5427/u6587/queues/u600e/u5b9a/u8bb8/u5feb/u660e /u884c/examples/u98de/u5916/u6811/u00009/u6d3b/u90e8/u95e8/u65e0/u5f80/u8239/u671b/examples/Examples /queues/u5458/u673a/u66f4/u4e5d/u60a8/u6bcf/u98ce/u7ea7/u8ddf/u7b11/queues/u76f4/queues/u591c /users/u9636/u8fde/u8f66/u91cd/u4fbf/u6597/users/u54ea/u5316/u592a/users/u793e/users/u58eb/u8005/users/u77f3 /u65e5/u51b3/u767e/queues/u62ff/queues/u672c/u601d/u89e3/queues/u6751/queues/u96be/u65e9/u8bba/u5417 /u6839/u5171/u8ba9/u76f8/u7814/u4eca/latest/u5750/u63a5/latest/u89c9/latest/u53cd/latest/u8bb0/u5c06/latest /users/u4e89/u9886/u6216/users/u5757/users/u52a0/u811a/u7d27/u7231/u7b49/u4e60/u9635/u6015 /queues/u9752/u534a/u706b/u6cd5/u9898/u5efa/u8d76/u4f4d/u5531/queues/u5973/queues/u51c6/queues/u56e2/queues /u79bb/u8272/Jun/u79d1/u5012/u775b/u5229/u4e16/u521a/u4e14/u7531/u9001/Jun/u8868/Jun/u6574 /examples/u54cd/u96ea/u6d41/u672a/u573a/u8be5/u5e76/u5e95/u6df1/u523b/examples/Examples /users/u53e4/users/u544a/users/u62c9/u540d/u5440/u571f/u6e05/u9633/users/u53f2/u6539/users/u753b/u9020/u5634 /u6b64/u6cbb/u5317/u5fc5/latest/u83dc/u722c/u7761/u5174/u5f62/u91cf/u54b1/u89c2 /u82e6/queues/u901a/u51b2/u5408/u7834/u53cb/u5ea6/u672f/u996d/u516c/u65c1/queues/u6781/queues/u67aa/queues /users/u91ce/u575a/users/u6536/users/u81f3/users/u57ce/u52b3/u843d/u94b1/users/u56f4/users/u80dc/u6559/u70ed/u5c55 /u6b4c/u7c7b/u6e10/latest/u6570/latest/u6027/u97f3/latest/u9645/u65e7/latest/u5e2e/u5566/u53d7/u7cfb /examples/u8df3/u975e/u4f55/u725b/u53d6/examples/u6133/examples/u79cd/u88c5/u9876/u6025/u6797/u505c/examples/Examples /u8863/u822c/u62a5/u53f6/u538b/u6162/u53d4/u80cc/u7ec6 ";
// Length of alternative Chinese Characters
Int length = base. Length ();

// Create memory image
Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
// Obtain the image Context
Graphics G = image. getgraphics ();

// Create a random class instance
Random random = new random ();

// Set the background color of the image (because it is a background, so it is light)
G. setColor (getRandColor (random, 200,250 ));
G. fillRect (0, 0, width, height );

// Alternative font
String [] fontTypes = {"/u5b8b/u4f53", "/u65b0/u5b8b/u4f53", "/u9ed1/u4f53", "/u6977/u4f53 ", "/u96b6/u4e66 "};
Int fontTypesLength = fontTypes. length;

// Add noise to the image background
G. setColor (getRandColor (random, 160,200 ));
G. setFont (new Font ("Times New Roman", Font. PLAIN, 14 ));
For (int I = 0; I <6; I ++)
{
G. drawString ("************************************* ********", 0, 5 * (I + 2 ));
}

// Obtain the Random verification code (6 Chinese characters)

// Save the Generated Chinese character string
String sRand = "";
For (int I = 0; I <6; I ++)
{
Int start = random. nextInt (length );
String rand = base. substring (start, start + 1 );
SRand + = rand;

// Set the font color
G. setColor (getRandColor (random, 10,150 ));
// Set the font
G. setFont (new Font (fontTypes [random. nextInt (fontTypesLength)], Font. BOLD, 18 + random. nextInt (6 )));
// Draw the Chinese character to the image
G. drawString (rand, 24 * I + 10 + random. nextInt (8), 24 );
}

// Save the authentication code to the session
Session. setAttribute ("hanzirand", sRand );

G. dispose ();

// Output the image to the page
ImageIO. write (image, "JPEG", response. getOutputStream ());
%>

Save the above two files as image. jsp (numbers) and Hanzi. jsp (Chinese characters). Then we compile a test file test. jsp for testing. The source code is as follows:

<% @ Page contenttype = "text/html; charset = gb2312" %>
<%
String incode = (string) request. getparameter ("Code ");
String rightcode = (string) Session. getattribute ("numrand"); // hanzirand
If (incode! = NULL & rightcode! = NULL)
If (rightcode. Equals (incode ))
{
Out. println ("the verification code is entered correctly! ");
}
Else
{
Out. println ("the verification code is incorrect. Please enter it again! ");
}

%>

<Form action = "test. jsp" method = "Post">
Enter the verification code: // if you want to use the Chinese character verification code, replace it with Hanzi. jsp.

<Input type = "text" name = "code">
<Input type = "submit" value = "verify"> </form>

Run test. jsp to view the result.

Tags:

 

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.