Settings for servlet verification codes

Source: Internet
Author: User
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".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.