A simple example of implementing JSP verification code _JSP programming

Source: Internet
Author: User
Tags rand set background

Copy Code code as follows:

<%@ page language= "java" pageencoding= "GBK"%>
<%@ page contenttype= image/jpeg "import=" java.awt.*,
java.awt.image.*,java.util.*,javax.imageio.*%>
<%!
Color getrandcolor (int fc,int BC) {///given range get random color
        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 an 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 Random Class
Random Random = new Random ();

Set Background color
G.setcolor (Getrandcolor (200,250));
G.fillrect (0, 0, width, height);

Set font
G.setfont (New Font ("Times New Roman", font.plain,18));

Draw a border
G.setcolor (New Color ());
G.drawrect (0,0,width-1,height-1);

Randomly generated 155 lines of interference, so that the image of the authentication code is not easily detected 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 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), 20+random.nextint (a), 20+random.nextint (110));
The call function comes out with the same color, possibly because the seed is too close, so it can only be generated directly
g.DrawString (rand,13*i+6,16);
}

Save authentication Code in session
Session.setattribute ("Rand", SRand);


Image entry into force
G.dispose ();

Output image to Page
Imageio.write (Image, "JPEG", Response.getoutputstream ());
Out.clear ();

%>

Page reference:

Copy Code code as follows:

<TD width= "valign=" bottom "><div align=" left "></div></td>

Js:

Copy Code code as follows:

This.src= ' index/image.jsp? ' +math.random ();

Login time to obtain the code:

Copy Code code as follows:

String randb = (string) request.getsession (). getattribute ("Rand");

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.