Image.jsp of the drawing:
<%@ page contenttype= "image/jpeg;charset=gb2312" import= java.awt.*, Java.awt.image.*,java.util.*,javax.imageio . * "%> <%!
Color Getrandcolor (int fc, int BC) {//The given range gets the random color 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);
}%> <%//Settings page does not cache Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);
Create image int width = 20 in memory;
BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);
Gets the graphics context Graphics g = image.getgraphics ();
Generate Random class Random Random = new Random ();
Set Background color G.setcolor (Getrandcolor (200, 250));
G.fillrect (0, 0, width, height);
Set 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);
Randomly generated 155 interference lines, so that the authentication code in the image is not easy to be detected by other programs G.setcolor (Getrandcolor (160, 200));
for (int i = 0; i < 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 r = Request.getparameter ("random");
Take the randomly generated authentication code (4 digits) String rand = "";
if (r!= null) {for (int i = 0; i < 4; i++) {rand = r.substring (i, i + 1);
Display the authentication code to the image G.setcolor (new Color (+ Random.nextint (), + random.nextint (110));
The call function comes out with the same color, possibly because the seed is too close, so the g.drawstring can only be generated directly (rand, i + 6, 16);
}//The authentication code is deposited in session//session.setattribute ("random", SRand);
Image entry into force g.dispose ();
Output image to page imageio.write (image, "JPEG", Response.getoutputstream ());
Out.clear ();
out = Pagecontext.pushbody ();
%>JSP for home page:
<body onload= "Refresh ()" >
<form name= "form" onsubmit= "return Check ()" action= "User!login.action" method= "POST" > <table align= "cente R "Style=" margin-top:120px; "> <tr> <td height=" align= "center" ><font color= "#FFFFFF" ><strong> User name: </strong > </font> </td> <td width= "181" Height=54><input id= "account" Name= "Uservo" . Account "> </TD> </tr> <tr> <td height=" ><font color= "#FFFFFF" >& Lt;strong> Code:</strong> </font> </td> <td width= "181" HEIGHT=54>&L
T;input id= "password" type= "password" name= "Uservo.password" ></TD> </tr> <tr> <TD height= "si" align= "center" ><font color= "#FFFFFF" ><strong> Verification Code: </strong> </font > </td> <td><input type=text name=rand id= "random" maxlength=4 size=7> & Lt;imG align= "Top" id= "randomimg" src= "image.jsp" onclick= "refresh ()"/> </td> </tr> < Tr> <td colspan= "2" align=center><br> <input type=submit value= landing > <stron G><font color= "White" ><input type= "button" value= "register" onclick= "window.location.href=" register . jsp ' "> </font> </strong> </td> </tr> </table> </form> <scrip T type= "Text/javascript" >//Generate random verification code var chars = [' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ',
' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ';
function generatemixed (n) {var res = "";
for (var i = 0; i < n; i++) {var id = Math.ceil (math.random () * 35);
res = Chars[id];
return res;
} var random = "";
function Refresh () {random = generatemixed (4); $ ("#randomImg"). attr ("SrC "," image.jsp "+ math.random () +" &random= "+ random);
The function check () {if ($ ("#account"). val () = "") {alert ("Please enter username!");
Form.account.focus ();
return false;
} if ($ ("#password"). val () = "") {alert ("Please enter password!");
Form.password.focus ();
return false;
} if ($ ("#random"). val () = "") {alert ("Verify code cannot be empty!");
Form.random.focus ();
return false;
} if ($ ("#random"). Val (). toUpperCase ()!= random) {alert ("Incorrect authentication Code!");
Form.random.focus ();
return false; }} </script>
</body>