JSP generation verification code source program

xiaoxiao2021-03-06  39

<% @ Page ContentType = "Image / JPEG" Import = "java.awt. *,

Java.awt.Image. *, java.util. *, javax.imageio. * "%>

<%

/ / Create image in memory

INT width = 60, height = 20;

BufferedImage Image = New BufferedImage (Width, Height,

BufferedImage.Type_INT_RGB);

/ / Get the graphical context

Graphics g = image.getgraphics ();

// Set the background color

G.SetColor (New Color (0xDCDCDC));

g.fillRect (0, 0, Width, Height);

// Sea Box

g.setcolor (color.ble);

g.drawRect (0, 0, Width-1, Height-1);

// Take the randomly generated authentication code (4 digits)

String Rand = Request.getParameter ("Rand");

Rand = rand.substring (0, rand.indexof ("."));

Switch (rand.length ())

{

Case 1: Rand = "000" rand; Break;

Case 2: Rand = "00" rand; Break;

Case 3: Rand = "0" rand; Break;

Default: rand = rand.substring (0, 4); Break;

}

// Deposit the authentication code into the session

Session.SetaTRibute ("rand", rand);

// Display the authentication code to the image

g.setcolor (color.ble);

Integer tempnumber = new integer (rand);

String numberstr = TEMPNUMBER.TOSTRING ();

G.SetFont (New Font ("Atlantic Inline", Font.Plain, 18));

String str = numberstr.substring (0, 1);

g.drawstring (STR, 8, 17);

Str = NumBerstr.SUBSTRING (1, 2);

g.drawstring (STR, 20, 15);

Str = NumBerstr.substring (2, 3);

g.drawstring (STR, 35, 18);

Str = NumBerstr.substring (3, 4);

g.drawstring (STR, 45, 15);

// randomly generate 88 interference points, so that the authentication code in the image is not easy to be probed by other programs

Random Random = new random ();

For (int i = 0; i <20; i )

{

INT x = random.nextint (width);

INT Y = random.nextint (height);

g.drawoval (X, Y, 0, 0);

}

// Image take effect

g.dispose ();

// Output image to page

Imageio.write (Image, "JPEG", response.getOutputStream ());

%>

转载请注明原文地址:https://www.9cbs.com/read-75341.html

New Post(0)