Image. jsp -------------------------------- Jsp page for Generating Random verification code Images
The Code is as follows:
Copy codeThe Code is as follows: <% @ page contentType = "image/jpeg" import = "java. awt .*,
Java. awt. image. *, java. util. *, javax. imageio. * "%>
<%!
Color getRandColor (int fc, int bc)
{
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 );
}
%>
<%
Out. clear (); // This sentence is for the resin server. If it is tomacat, do not use this sentence.
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 ());
%>
Logic. jsp ---------------------------------- on the login page, enter the verification code here and submit and verify
The Code is as follows:Copy codeThe Code is as follows: <% @ page contentType = "text/html; charset = gbk" %>
<% @ 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 -------------------------- used to verify that the entered verification code is correct
The Code is as follows:Copy codeThe Code is as follows: <% @ 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> ");
}
%>
The above three JSP pages are placed in the directory of the same level as the WEB-INF, if you put in a different folder, then the path of the relevant page inside yourself for a slight change on it.
Tip: Exceptions may occur in Tomcat 5 or earlier versions.