Java implementation of verification code--jsp

Source: Internet
Author: User
Tags set background

<%@ page language= "java" pageencoding= "UTF-8"%><%@ page contenttype= "Image/jpeg"Import= "Java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%><%!Color Getrandcolor (intFcintBC) {//get random colors for a given rangeRandom random =NewRandom (); if(FC > 255) FC= 255; if(BC > 255) BC= 255; intr = FC + random.nextint (BC-FC); intg = FC + random.nextint (BC-FC); intB = fc + random.nextint (BC-FC); return NewColor (R, G, b); }%><%//Settings page does not cacheResponse.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); Response.setdateheader ("Expires", 0); //generate random Verification codeRandom random =NewRandom (); String SRand= "";  for(inti = 0; I < 4; i++) {String rand= String.valueof (Random.nextint (10)); SRand+=Rand; }    //Save authentication Code to sessionSession.setattribute ("Check_number_key", SRand); //Create an image in memory    intwidth = 55; intHeight = 20; BufferedImage Image=Newbufferedimage (width, height, bufferedimage.type_int_rgb); //Get the graphics contextGraphics g =Image.getgraphics (); //Set Background colorG.setcolor (Getrandcolor (200, 250)); G.fillrect (0, 0, width, height); //Set FontG.setfont (NewFont ("Serif", Font.center_baseline, 16)); //Draw Border//G.setcolor (New Color (1)); //G.drawrect (0,0,width-1,height-1); //randomly generates 155 lines of interference so that the authentication code in the image is not easily detected by other programsG.setcolor (Getrandcolor (160, 200));  for(inti = 0; I < 100; i++) {        intx =random.nextint (width); inty =random.nextint (height); intXL = Random.nextint (12); intYL = Random.nextint (12); G.drawline (x, y, x+ XL, Y +yl); }     for(inti = 0; I < 4; i++) {String rand= Srand.substring (I, i + 1); //Display the authentication code to the imageG.setcolor (NewColor (+ random.nextint (110), 20 +random. Nextint (), + 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 directlyg.DrawString (Rand, * i + 6, 16); }    //Image EffectiveG.dispose (); //output image to page    Try{imageio.write (image,"JPEG", Response.getoutputstream ()); } Catch(Exception e) {} out.clear (); out=pagecontext.pushbody ();%>

Java implementation of verification code--jsp

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.