The verification code is an effective method for login verification. The following describes how to use it in jsp. The following describes only the simplest implementation method. We will add Ajax to make verification more formal later.
This article uses the jsp page as the carrier. That is to say, the image that generates the verification code is directly placed in jsp for implementation. Interested friends can be placed in servlet for easier management.
1. image. jsp --- Jsp page for Generating Random verification code Images
<% @ 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 ());
%>
The effect is as follows:
2. logic. jsp ---- log on to the page. Enter the verification code and submit it for verification. The page loads image. jsp for verification.
<% @ 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>
The effect is as follows:
3. validate. jsp ---- used to verify that the entered verification code is correct
<% @ 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 effect is as follows;
Because the verification code I entered earlier is incorrect, It is shown above.
Author: wangjinyu501