Tomcat webapps to create a directory validate, build a WEB-INF Directory, which creates a web. xml file, the Code does not need to be written, there is a statement on the line: Web. xml: <? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> </web-app> and then create three files under the validate Directory: image. JSP: The Verification Code image used to generate four random numbers <% @ 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 the memory
Int width = 60, Height = 20;
Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb); // obtain the image Context
Graphics G = image. getgraphics (); // generates a random class
Random random = new random (); // sets 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 (155, width-1, height-1); // interference lines are randomly generated, 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 (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 of the called function is the same. It may be because the seed is too close and can only be directly generated.
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 ());
%> Create A. jsp, mainly form submission (LOGIN): 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> Create another check. jsp to compare the entered verification code with the verification code number generated by the image. If the verification code is the same, the verification code is successful. If the verification code is different, the Verification Code fails. 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 )){
%>
The input is the same. authentication is successful!
<%
} Else {
%>
Different inputs, authentication failed!
<%
}
%>
</Body>
</Html> okay. start Tomcat and try http: // localhost: 8080/validate/a. jspok! Simple and practical!