Vcode. jsp Verification Code

Source: Internet
Author: User

If you want to reference it as an image, OK

 

 

<% @ Page contenttype = "image/JPEG" %> <br/> <% @ page import = "Java. AWT. * "%> <br/> <% @ page import =" Java. AWT. image. * "%> <br/> <% @ page import =" Java. util. * "%> <br/> <% @ page import =" javax. imageIO. * "%> <br/> <% @ page import =" Java. io. outputstream "%> </P> <p> <%! <Br/> color getrandcolor (int fc, int BC) {// obtain a random color from a given range <br/> random = new random (); <br/> If (FC> 255) fc = 255; <br/> If (BC> 255) BC = 255; <br/> int r = FC + random. nextint (BC-Fc); <br/> int G = FC + random. nextint (BC-Fc); <br/> int B = FC + random. nextint (BC-Fc); <br/> return new color (R, G, B ); <br/>}< br/>%> <br/> <% <br/> // set the page to not cache <br/> response. setheader ("Pragma", "No no-Cache"); <br/> response. setheader ("cache-control", "No-Cache"); <br/> response. setdateheader ("expires", 0); </P> <p> // create an image in the memory <br/> int width = 60, Height = 20; <br/> bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb); </P> <p> // obtain the image context <br/> graphics G = image. getgraphics (); </P> <p> // generate a random class <br/> random = new random (); </P> <p> // set the background color <br/> G. setcolor (getrandcolor (200,250); <br/> G. fillrect (0, 0, width, height); </P> <p> // set the font <br/> G. setfont (new font ("Times New Roman", Font. plain, 18); </P> <p> // generates 155 random interference lines, make the authentication code in the image difficult to be detected by other <SPAN class = 'wp _ keywordlink '> Programs </span> <br/> G. setcolor (getrandcolor (160,200); <br/> for (INT I = 0; I <155; I ++) <br/>{< br/> int x = random. nextint (width); <br/> int y = random. nextint (height); <br/> int XL = random. nextint (12); <br/> int yl = random. nextint (12); <br/> G. drawline (X, Y, x + XL, Y + yl); <br/>}</P> <p> // obtain the random ID code (4 digits) <br/> string srand = ""; <br/> for (INT I = 0; I <4; I ++) {<br/> string Rand = string. valueof (random. nextint (10); <br/> srand + = rand; <br/> // display the authentication code in the image <br/> G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110); <br/> // The color from the call function is the same, probably because the seed is too close, so it can only be directly generated <br/> G. drawstring (RAND, 13 * I + 6, 16); <br/>}</P> <p> // Save the authentication code to the session <br/> session. setattribute ("RAND", srand); </P> <p> // draw an image <br/> G. dispose (); </P> <p> outputstream OS = response. getoutputstream (); <br/> // output the image to the page <br/> ImageIO. write (image, "Jpeg", OS); </P> <p> // clear the out object of JSP to avoid the out object in servelet and the response above. getoutputstream conflict <br/> out. clear (); <br/> out = pag. pushbody (); </P> <p >%> <br/>

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.