Generate 4 random numbers and clutter background pictures, the number and background color will change, server-side refresh (with History.go (-1) will also change)
Prototype reference Alibaba Http://china.alibaba.com/member/showimage
The file that produces the CAPTCHA picture-----image.jsp
<%@ page contenttype= "image/jpeg" import= "java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%>
<%!
Color getrandcolor (int fc,int BC) {//given range get 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);
}
%>
<%
//Set Page not cached
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);
Create an image in memory
int width=60, height=20;
BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);
Get 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 lines of interference, so that the image of the authentication code is not easily 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);
}
Take a randomly generated authentication code (4 digits)
String srand= "";
for (int i=0;i<4;i++) {
String rand=string.valueof (Random.nextint (10));
Srand+=rand;
Display the authentication code in the image
G.setcolor (new color (20+random.nextint, 20+random.nextint (), 20+random.nextint (110));//Call function out of the same color, May be because the seed is too close, so can only be directly generated
g.DrawString (rand,13*i+6,16);
}
Save authentication Code in session
Session.setattribute ("Rand", SRand);
Image entry into force
G.dispose ();
Output image to Page
Imageio.write (Image, "JPEG", Response.getoutputstream ());
%>
---------------file---------a.jsp using the captcha picture------------------------------------
<%@ page contenttype= "text/html;charset=gb2312"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title> Authentication 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" >
<body>
<form method=post action= "check.jsp" >
<table>
<tr>
<TD align=left> System-generated authentication code:</td>
<td></td>
</tr>
<tr>
<TD align=left> Enter the authentication code above:</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>
-----------------Validated pages----------check.jsp
<%@ page contenttype= "text/html; charset=gb2312 "language=" java "import=" java.sql.* "errorpage=" "%>"
<title> Authentication Code 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" >
<body>
<%
String rand = (string) session.getattribute ("Rand");
String input = Request.getparameter ("Rand");
%>
The authentication code generated by the system is: <%= Rand%><br>
The authentication code you have entered is: <%= input%><br>
<br>
<%
if (rand.equals (input)) {
%>
<font color=green> Input the same, authentication success! </font>
<%
} else {
%>
<font color=red> input is different, authentication failed! </font>
<%
}
%>
</body>