JSP Verification Code Generation

Source: Internet
Author: User

Call method: Use Image labels on the JSP page to directly call the following labels: Code
you only need to send the code to the area where the verification code is displayed)
<% @ Page contenttype = "image/JPEG" Import = "Java. AWT. *, Java. AWT. image. *, Java. util. *, javax. imageIO. * "%>
<%!
Public static string code = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ";
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 the memory and set the image display size
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 identification code in the image hard to be used by others Program Detected
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 (consisting of digits and letter leaders)
String srand = "";
For (INT I = 0; I <4; I ++ ){
Int Rand = random. nextint (62 );
Srand + = string. valueof (code. charat (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 (string. valueof (code. charat (RAND), 13 * I + 6, 16 );
}
// Save the authentication code to the session
Session. setattribute ("RAND", srand );
// The image takes effect
G. Dispose ();
// Output the image to the page
ImageIO. Write (image, "Jpeg", response. getoutputstream ());
%>
. Below is a test. jsp to test the verification code generation, and then submit it to check. jsp for processing.
<% @ Page contenttype = "text/html; charset = gb2312" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> certificate code input page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Meta HTTP-EQUIV = "Pragma" content = "no-Cache">
<Meta HTTP-EQUIV = "cache-control" content = "no-Cache">
<Meta HTTP-EQUIV = "expires" content = "0">
</Head>
<Body>
<Form method = post action = "check. jsp">
<Table>
<Tr>
<TD align = left> the verification code generated by the system: </TD>
<TD> </TD>
</Tr>
<Tr>
<TD align = left> enter the above verification code: </TD>
<TD> <input type = text name = Rand maxlength = 4 value = ""> </TD>
</Tr>
<Tr>
<TD colspan = 2 align = center> <input type = submit value = "Submit detection"> </TD>
</Tr>
</Form>
</Body>
</Html>
. The following is a check. jsp
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
<HTML>
<Head>
<Title> verification page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Meta HTTP-EQUIV = "Pragma" content = "no-Cache">
<Meta HTTP-EQUIV = "cache-control" content = "no-Cache">
<Meta HTTP-EQUIV = "expires" content = "0">
</Head>
<Body>
<%
String Rand = (string) Session. getattribute ("RAND ");
String input = request. getparameter ("RAND ");
%>
The verification code generated by the system is: <% = Rand %> <br>
The verification code you entered is: <% = input %> <br>
<Br>
<%
If (RAND. Equals (input )){
%>
<Font color = green> the input is the same and the authentication is successful! </Font>
<%
} Else {
%>
<Font color = Red> If the input is different, authentication fails! </Font>
<%
}
%>
</Body>
</Html>
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.