Public class resonperandomimgdemo extends httpservlet {
Int width = 100;
Int Height = 30;
Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
Response. setcontenttype ("text/html; charset = UTF-8 ");
// Obtain the bufferedimage object and set the size and type of the canvas.
Bufferedimage IMG = new bufferedimage (width, height, bufferedimage. type_int_rgb );
// Obtain the Drawing Object
Graphics2d G = (graphics2d) IMG. getgraphics ();
// Set the background color
Setbackgroudcolor (g );
// Generate a random number
Getnum (g );
// Write the image to the browser
Response. setcontenttype ("image/JPEG ");
ImageIO. Write (IMG, "jpg", response. getoutputstream ());
}
Private void getnum (graphics2d g ){
String STR = "0123456789 qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm ";
G. setcolor (color. Blue );
For (INT I = 0; I <4; I ++ ){
// Generate a random number. % 30 is also the rotation angle to generate a random number between-30-30.
Int degee = new random (). nextint () % 30;
// Rotate
G. Rotate (degee * Math. PI/180, 20 + (I * 12), 15 );
G. setfont (new font ("", Font. Bold, 20 ));
G. drawstring (Str. charat (new random (). nextint (Str. Length () + "", 20 + (I * 12), 15 );
// Rotate back
G. Rotate (-degee * Math. PI/180, 20 + (I * 12), 15 );
}
}
Private void setbackgroudcolor (graphics2d g ){
G. setbackground (color. White );
G. fillrect (0, 0, width, height );
}
Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
Doget (request, response );
}
}
// JSP page
// servlettest/servlet/resonperandomimgdemo obtain information on the servelt page