This article illustrates the method of JSP implementation of simple verification code. Share to everyone for your reference. Specifically as follows:
There is only one file, which can be invoked directly in the SRC attribute of the , for form security validation for various projects
JSP page code:
<%@ 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);
%> <% 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), 20+random.nextint (a), 20+random.nextint (110));
g.DrawString (rand,13*i+6,16);
} session.setattribute ("Rand", SRand);
G.dispose ();
Imageio.write (Image, "JPEG", Response.getoutputstream ());
Response.getoutputstream (). Flush ();
Response.getoutputstream (). Close ();
Out.clear ();
Out=pagecontext.pushbody ();
%>
I hope this article will help you with your JSP programming.