Joke Collection Website - Public benefit messages - Java swing random verification code

Java swing random verification code

Public? Static electricity Invalid? processRequest(http servlet request? Request,

HttpServletResponse? Response)? Throwing? ServletException,? IOException? {

?

? response . setcontenttype(" image/JPEG ");

? Response.setHeader("Pragma ","no cache ");

? Response.setheader ("cache-control", "no cache");

? response . setdate header(" Expires ",0);

? HttpSession? Conversation? =? request . getsession();

?

? int? Width? =? 60,? Height? =? 20;

?

? BufferedImage? Image? =? New? BufferedImage (width, height,

BufferedImage。 TYPE _ INT _ RGB);

?

? //? Get graphic context

? Graphics? g? =? image . get graphics();

?

? //? Generate random class

? Random? Random? =? New? random();

?

? //? Set the background color.

? g.setColor(getRandColor(200,? 250));

? g.fillRect(0,? 0,? Width? Height);

?

? //? Set font

? G.setFont (new? Font("Times? New? Roman? Font. Flat and faint? 18));

?

? //? 155 interference line is generated randomly, which makes the authentication code in the image difficult to be detected by other programs.

? g . set color(getRandColor( 160,? 200));

? For what? (int? Me? =? 0; ? Me? & lt? 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 the randomly generated authentication code (4 digits)

? String? sRand? =? "";

? For what? (int? Me? =? 0; ? Me? & lt? 4; ? i++)? {

String? Rand? =? string . value of(random . nextint( 10));

sRand? +=? Rand;

//? Display the verification code to the image.

G.setColor (new? Color (20? +? random.nextInt( 1 10),? 20? +? at will

? . nextInt( 1 10),? 20? +? random . nextint( 1 10))); //? Call functions are all the same color, probably because the seeds are too close, so they can only be generated directly.

G. draw rope (rand, 13? *? Me? +? 6,? 16);

? }

?

? //? Store the authentication code in the session.

? session.setAttribute("rand ",sRand);

? //? Image verification

? g . dispose();

? ServletOutputStream? responseOutputStream? =? response . get output stream();

? //? Output the image to the page

? ImageIO.write(image,? “JPEG”,? responseOutputStream);

?

? //? Close the input stream below!

? responseoutputstream . flush();

? responseoutputstream . close();

? }

?

Static electricity Color? getRandColor(int? fc,? int? bc)? {

? //? Gets a random color in a given range.

? Random? Random? =? New? random();

? What if? (fc? & gt? 255)

fc? =? 255;

? What if? (BC? & gt? 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);

? }

?

? /**?

? *? Handle? That? HTTP? Get? Method. ?

? *

? *? @param? Request?

? *servlet? Request?

? *? @param? Response?

? *servlet? Response?

? */?

? Protected? Invalid? doGet(HttpServletRequest? Request,

HttpServletResponse? Response)? Throwing? ServletException,? IOException? {

? ProcessRequest (request, response);

? } This is the verification code used in my web. You can change it.