Four random numbers and messy background images are generated. The numbers and background colors change, and the server refreshes (with history. go (-1)
Prototype reference ALIBABA http://china.alibaba.com/member/showimage
File that generates the verification code image ----- image. jsp
<% @ 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 ("rand", sRand );
// The image takes effect
G. dispose ();
// Output the image to the page
ImageIO. write (image, "JPEG", response. getOutputStream ());
%>
--------------- File --------- a. jsp ------------------------------------
<% @ 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>
----------------- Verification page ---------- 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>