<%@ 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