Home>
<%@page contenttype="image/jpeg" import="java.awt. *, Java.awt.image. *, Java.util. *, Javax.imageio. *"%>
<%!
color getrandcolor (int fc, int bc) {//Get random color for a given range
    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 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 a random class
random random=new random ();
//Set the background color
g.setcolor (getrandcolor (200,250));
g.fillrect (0, 0, width, height);
//Set the font
g.setfont (new font ("times new roman", font.plain, 18));
//Draw the border
//g.setcolor(new color ());
//g.drawrect(0,0,width-1,height-1);
//randomly generate 155 interference lines,Make the authentication code in the image difficult to detect 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 rand=request.getparameter ("rand");
//rand=rand.substring (0, rand.indexof ("."));
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 (110), 20 + random.nextint (110), 20 + random.nextint (110)));//The color of the function is the same,May be because the seeds are too close,So it can only be generated directly
  g.drawstring (rand, 13 * i + 6,16);
}
//Store the authentication code in the session
session.setattribute ("rand", srand);
//image takes effect
g.dispose ();
//output image to page
imageio.write (image, "jpeg", response.getoutputstream ());
out.clear ();
out=pagecontext.pushbody ();
%>
jsp
  • Previous Method for uploading excel and inserting excel into database by JSP
  • Next C # method of drawing a graph