JSP color image verification code three steps
Image. jsp -------------------The image that generates the verification code immediatelyJSPPage
<% @ page contenttype = "image/JPEG" Import = "Java. AWT. *,
JAVA. AWT. image. *, Java. util. *, javax. imageIO. * "%>
<%!
color getrandcolor (int fc, int BC)
{< br> 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);
}< BR >%>
<%< br> out. clear (); // This statement is applicable to resin servers. If tomacat is used, do not use this statement
response. setheader ("Pragma", "No-Cache");
response. setheader ("cache-control", "No-Cache");
response. setdateheader ("expires", 0);
int width = 60, Height = 20;
bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb);
Graphics G = image. getgraphics ();
Random random = new random ();
G. setcolor (getrandcolor (200,250 ));
G. fillrect (0, 0, width, height );
G. setfont (new font ("Times New Roman", Font. Plain, 18 ));
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 );
}
String srand = "";
For (INT I = 0; I <4; I ++ ){
String Rand = string. valueof (random. nextint (10 ));
Srand + = rand;
G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110 )));
G. drawstring (RAND, 13 * I + 6, 16 );
}
// Save the authentication code to the session
Session. setattribute ("RAND", srand );
G. Dispose ();
ImageIO. Write (image, "Jpeg", response. getoutputstream ());
%>
Import. jsp ------------------------------------------------------------------------------ verify the image import page
<% @ Page Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> User Logon </title>
<Script language = "JavaScript">
Function LoadImage (){
Document. getelementbyid ("randimage"). src = "image. jsp? "+ Math. Random ();
}
</SCRIPT>
</Head>
<Body>
<Table width = "256" border = "0" cellpadding = "0" cellspacing = "0">
<! -- Dwlayouttable -->
<Form action = "Validate. jsp" method = "Post" name = "loginform">
<Tr>
<TD width = "118" Height = "22" valign = "Middle" align = "center"> <input type = "text" name = "RAND" size = "15"> </TD>
<TD width = "138" valign = "Middle" align = "center"> </TD>
</Tr>
<Tr>
<TD Height = "36" colspan = "2" align = "center" valign = "Middle"> <a href = "javascript: LoadImage (); "> <font class = PT95> cannot see me </font> </a> </TD>
</Tr>
<Tr>
<TD Height = "36" colspan = "2" align = "center" valign = "Middle"> <input type = "Submit" name = "login" value = "Submit"> </TD>
</Tr>
</Form>
</Table>
</Body>
</Html>
Validate. jsp ---------------------------------------------------------------------------- verification code page
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
<%
String Rand = (string) Session. getattribute ("RAND ");
String input = request. getparameter ("RAND ");
If (RAND. Equals (input )){
Out. Print ("<SCRIPT> alert ('verification passed! '); </SCRIPT> ");
} Else {
Out. Print ("<SCRIPT> alert ('Enter the correct verification code! '); Location. href = 'login. jsp'; </SCRIPT> ");
}
%>