Validation code generation and invocation method of JSP pages

Source: Internet
Author: User
Tags set background

The first is to generate the verification code:

Package XX. Xx
Import java.io.*;
Import java.util.*;
Import javax.imageio.*;
Import java.awt.*;
Import java.awt.image.*;
public class imageensure ... {
Public Imageensure () ... {
}
Private char maptable[] = ... {' 0 ', ' 1 ', ' 2 ', ' 3 ',
' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};
Public String getensure (int width, int height, outputstream os) ... {
if (width <= 0)
width = 60;
if (height <= 0)
height = 20;
BufferedImage image = new BufferedImage (width, height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Get Graphics context
Graphics g = image.getgraphics ();
Set Background color
G.setcolor (New Color (0xDCCCCC));
G.fillrect (0, 0, width, height);
Draw a border
G.setcolor (Color.Black);
G.drawrect (0, 0, width-1, height-1);
Take a randomly generated authentication code
String strensure = "";
4 represents 4-bit captcha
for (int i = 0; i < 4; ++i) ... {
Strensure + = maptable[(int) (Maptable.length * Math.random ())];
}
Display the authentication code in the image
G.setcolor (color.red);
G.setfont (New Font ("Atlantic Inline", Font.plain, 14));
The exact coordinates of the painting
String str = strensure.substring (0, 1);
g.DrawString (str, 8, 14);
str = strensure.substring (1, 2);
g.DrawString (str, 20, 15);
str = strensure.substring (2, 3);
g.DrawString (str, 35, 18);
str = strensure.substring (3, 4);
g.DrawString (str, 45, 15);
Releasing the graphics context
G.dispose ();
Try ... {
Output image to Page
Imageio.write (Image, "JPEG", OS);
catch (IOException e) ... {
Return "";
}
return strensure;
}
}

Related Article

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.