Joke Collection Website - Blessing messages - JSP verification code problem!

JSP verification code problem!

Start servlet service

home page

& ltimg src="RandomCodeCtrl"/>。

web.xml

& ltservlet & gt

& ltservlet-name & gt; RandomCodeCtrl & lt/servlet-name & gt;

& ltservlet-class & gt; com . chain way . util . randomcodectrl & lt; /servlet-class & gt;

& lt Load & GT4 at startup & lt/load-on-startup >

& lt/servlet & gt;

& ltservlet mapping & gt

& ltservlet-name & gt; RandomCodeCtrl & lt/servlet-name & gt;

& lturl mode & gt/RandomCodeCtrl & lt; /URL-pattern & gt;

& lt/servlet-mapping & gt;

RandomCodeCtrl:

Package com.chainway.util;

Import java.io.ioexception;

Import javax.servlet.servletexception;

Import javax.servlet..chainway.util

Import java.awt.color;

Import java.awt.font;

Import java.awt.graphics;

Import java.awt.image.buffered image;

Import java.util.random;

Import javax.imageio.imageio;

Import javax.servlet.http.httpservletrequest;

Import javax.servlet.http.httpservletresponse;

Import javax.servlet.http.httpsession;

Public class RandomCode {

/**

* get a font at random

* @param Random random number

* @return Font returns the new font.

*/

Private synchronous font getsfont (random random) (

Returns the new font ("Fixedsys", font. CENTER_BASELINE, 18);

}

/**

* Returns a random color.

* @param int fc random number

* @param int bc random number

* @param Random random number

* @return Color returns a new color.

*/

Sync color getRandColor(int fc, int bc, Random random){

if(fc & gt; 255)fc = 255;

if(BC & gt; 255)BC = 255;

int r = fc+random . nextint(BC-fc-6);

int g = fc+random . nextint(BC-fc-4);

int b = fc+random . nextint(BC-fc-8);

Returns a new color (r, g, b);

}

/**

* generate random number pictures

*/

The public synchronization void getrandcode (HTTP Servlet Request, HttpServletResponse) throws an exception {

system . set property(" Java . awt . headless "," true ");

http session session = request . getsession();

Int width=80, height = 26// Set the picture size.

BufferedImage image = new buffered image (width, height, buffered image. TYPE _ INT _ RGB);

graphics g = image . get graphics();

Random Random = new Random();

g.fillRect(0,0,width,height); //Set the border

G.setFont ("Times New Roman", font. ROMAN_BASELINE, 18);

G. set color (get randcolor (111,133, random));

//Generate random lines

for(int I = 0; I< 1 1; i++){

int x = random . nextint(width);

int y = random . nextint(height);

int XL = random . nextint( 13);

int yl = random . nextint( 15);

g.drawLine(x,y,x+xl,y+yl);

}

//Generate random points

g . set color(getRandColor( 130, 150,random));

//Generate 5 random numbers

String sRand =

for(int I = 0; I<5; i++){

g . set font(gets font(random));

g . set Color(new Color(random . nextint( 10 1),random . nextint( 1 1 1),random . nextint( 12 1)));

//String rand = String . value of(getRandomString(random . nextint(36));

string rand = string . value of(getRandomString(random . nextint( 10));

sRand+= rand;

g.translate(random.nextInt(3),random.nextInt(3))。

G. rope drawing (rand, 13*i,16);

}

session . remove attribute(" Rand ");

session.setAttribute("Rand ",sRand);

g . dispose();

ImageIO.write(image," JPEG ",response . get output stream());

}

Common synchronization string getRandomString(int num){

string rand string = " 0 123456789 ";

//String rand String = " 0 123456789 abcdefghijklmnopqrstuvwxyz ";

Returns string.valueof (rand string.charat (num));

}

}

//landing judgment method

Get the verification code from the process and compare it.