public class Authimageservlet extends HttpServlet
{
private static final String Content_Type = "text/html; charset=gb2312 ";
Set the size and size of letters
Private font mfont = new Font ("Times New Roman", Font.plain, 17);
public void Init () throws Servletexception
{
Super.init ();
}
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);
}
public void Service (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException
{
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);
Indicates that the generated response is a picture
Response.setcontenttype ("Image/jpeg");
int width=100, height=18;
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 (1, 1, width-1, height-1);
G.setcolor (New Color (102,102,102));
G.drawrect (0, 0, width-1, height-1);
G.setfont (Mfont);
G.setcolor (Getrandcolor (160,200));
Draw random Lines
for (int i=0;i<155;i++)
{
int x = Random.nextint (width-1);
int y = random.nextint (height-1);
int xl = Random.nextint (6) + 1;
int yl = Random.nextint (12) + 1;
G.drawline (x,y,x + xl,y + yl);
}
Draw a random line from the other side
for (int i = 0;i < 70;i++)
{
int x = Random.nextint (width-1);
int y = random.nextint (height-1);
int xl = Random.nextint (12) + 1;
int yl = Random.nextint (6) + 1;
G.drawline (X,y,x-xl,y-yl);
}
Generates a random number and converts a random number to a letter
String srand= "";
for (int i=0;i<6;i++)
{
int itmp = Random.nextint (26) + 65;
Char ctmp = (char) itmp;
SRand + = string.valueof (ctmp);
G.setcolor (New Color (20+random.nextint, 20+random.nextint), 20+random.nextint (110));
g.DrawString (String.valueof (ctmp), 15*i+10,16);
}
HttpSession session = Request.getsession (true);
Session.setattribute ("Rand", SRand);
G.dispose ();
Imageio.write (Image, "JPEG", Response.getoutputstream ());
}
public void Destroy ()
{
}
}
And then, on the picture in the JSP, pass the src= "path to the servlet".