JSP Verification Code Encyclopedia of Chinese Verification Code

Source: Internet
Author: User
Tags rand

In the previous article, we introduced the process of generating digital verification code pictures in JSP, this article will continue to introduce the implementation of another validation code in JSP, that is, the implementation of Chinese verification code in JSP, the advantage of using Chinese verification code can improve the validity of verification, improve the security of verification, Because Chinese relative to the English or digital stroke structure is relatively complex, thereby increasing the analytical program to parse the code picture and read the verification information difficult. In this paper, the implementation process of Chinese verification is analyzed.

Second, the implementation of the JSP in the Chinese Code source code as follows:

chineseval.jsp
<%@ page pageencoding = "gb2312" contenttype= "image/jpeg" import = "javax.imageio.*,java.util.*,java.awt.image.*, Java.awt.* "%>
<%!
Get and generate random colors here
Color Getrandcolor (Random Random, int ff, int cc) {
if (FF > 255)
FF = 255;
if (cc > 255)
CC = 255;
int r = FF + random.nextint (CC-FF);
int g = ff + random.nextint (CC-FF);
int b = ff + random.nextint (CC-FF);
return new Color (R, G, b);
}%>
<%
Set JSP page no cache here
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);
Set the width of a picture
int width = 130;
Intheight = 30;
Set a random selection of Chinese characters, where the text content is too much, not listed, just for illustrative.
String base = "\u9752\u534a\u706b\u6cd5\u9898\u5efa\u8d76\u4f4d\u5531\u6d77\u4e03 \u5973\u4efb\u4ef6\u611f\u51c6\ u97f3\u7b54\u54e5\u9645\u65e7\u795e\u5ea7\u7ae0\u538b\u6162 \u53d4\u80cc\u7ec6 ... Omit text ... " ;
Set the number of alternative random characters
int length = Base.length ();
Create a cached image
BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);
Get image
Graphics g = image.getgraphics ();
Create an instance of a random function
Random Random = new Random (); Set the image background color here
G.setcolor (Getrandcolor (random, 188, 235));
G.fillrect (0, 0, width, height);
Set a random alternative font type
String[] Fonttypes = {"\u5b8b\u4f53", "\u65b0\u5b8b\u4f53",
"\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66"};
int fonttypeslength = Fonttypes.length;
Increase the noise in the picture background, increase the difficulty of the picture analysis
G.setcolor (Getrandcolor (random, 180, 199));
G.setfont (New Font ("Times New Roman", Font.plain, 14));
for (int i = 0; i < 4; i++) {
g.DrawString ("@*@*@*@*@*@*@*",
0, 5 * (i + 2));
}
Take a randomly generated verification code (4 characters)
Save the generated character string
String SRand = "";
for (int i = 0; i < 4; i++) {
int start = random.nextint (length);
String rand = base.substring (start, start + 1);
SRand + rand;
Set the color of the font on a picture
G.setcolor (Getrandcolor (random, 10, 150));
Set font formatting
G.setfont (New Font fonttypes[random.nextint (fonttypeslength)),
Font.Bold + random.nextint (6));
Draw the Chinese character to the validation picture
g.DrawString (Rand, * i + + random.nextint (8), 24);
///To deposit the verification code in S Ession
Session.setattribute ("Rand", SRand);
G.dispose ();
Output the image to a JSP page
Imageio.write (Image, "JPEG", Response.getoutputstream ());
Close the stream
Out.clear ();
Out=pagecontext.pushbody ();
%>

The above is the JSP generated in the Chinese verification Code process source analysis, from the Chinese as a verification code exists in the JSP page, there will be JSP Chinese garbled phenomenon, the next article will describe how to invoke the JSP code and the use of methods, and how to solve the JSP Chinese authentication Code garbled ask And other Java verification code implementations.

The following is a description of the garbled problem resolution for JSP Chinese authentication code and the method for invoking both of these authentication codes in the JSP.

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.