checknum.jsp
Copy Code code as follows:
<%@ page language= "java" import= "java.util.*,java.sql.*" pageencoding= "UTF-8"%>
<%@ page contenttype= "image/jpeg" import= "java.awt.*,java.awt.image.*,javax.imageio.*"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<%!
Color getrandcolor (int fc,int BC)//given range randomly selected colors
{
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);
}
%>
<%
Response.setheader ("Pragma", "No-cache");/set Page not buffered
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 ();//Get Image context
Random Random = new Random ();//Generate Random Object
G.setcolor (Getrandcolor (200,250));
G.fillrect (0,0,width,height);
G.setfont (New Font ("Times New Roman", font.plain,18);/Set Font
for (int i=0;i<155;i++)
{
int X=random.nextint (width);
int y=random.nextint (height);
int x1 = Random.nextint (12);
int y1 = Random.nextint (12);
G.drawline (X,Y,X+X1,Y+Y1);
}
Random generation of verification code
String SRand = "";
for (int i=0;i<4;i++)
{
String rand = string.valueof (Random.nextint (10));
Srand+=rand;
Display the captcha in the image
G.setcolor (New Color (20+random.nextint), 20+random.nextint (a), 20+random.nextint (110));
Set the color to display random numbers
g.DrawString (rand,13*i+6,16);
}
Store the CAPTCHA in session
Session.setattribute ("Rand", SRand);
Image effective
G.dispose ();
Output image to Page
Imageio.write (Image, "JPEG", Response.getoutputstream ());
Out.clear ();
out = Pagecontext.pushbody ();
%>
Can be passed directly under the login login.jsp: this format call;
Verification code: <input class= "Imgbutton" name= "Checknum" type= "text" size= "" "id =" Checknum "maxlength=" 4 "/>
<br/>
<input type= "Submit" name= "submit" value= "Login"/>