/************************************ Servlet page ** **********************************/
Package response;
Import java. AWT. color;
Import java. AWT. Font;
Import java. AWT. graphics;
Import java. AWT. graphics2d;
Import java. AWT. image. bufferedimage;
Import java. Io. ioexception;
Import java. util. Random;
Import javax. ImageIO. ImageIO;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
// Generate a random image (Verification Code)
Public class responsedemo4 extends httpservlet {
// Define the Image Width
Public static finals int width = 120;
// Define the Image Height
Public static final int Height = 30;
Protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
// Create an image in the memory
Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
// Obtain the image
Graphics G = image. getgraphics ();
// 1. Set the background color.
Setbackground (g );
// 2, set the border
Setborder (g );
// 3. Draw interference lines
Drawrandomline (g );
// 4, Xie Random Number
Drawrandomnum (graphics2d) g );
// 5. Write the image to the browser
Response. setcontenttype ("image/JPEG"); // notify the browser to open an image in JPEG format
// Do not cache the browser for sending header Control
Response. setdateheader ("expries",-1 );
Response. setheader ("cache-control", "No-Cache ");
Response. setheader ("Pragma", "No-Cache ");
ImageIO. Write (image, "jpg", response. getoutputstream ());
}
// Fill Chinese characters. Graphics does not rotate. Therefore, graphics2d is written here, But graphics2d has compatibility problems.
Private void drawrandomnum (graphics2d g ){
// Set the font color
G. setcolor (color. Red );
// Set the font to, bold, and 20
G. setfont (new font ("", Font. Bold, 20 ));
// Common Chinese Characters
String base = "\ u7684 \ u4e00 \ u4e86 \ u662f \ u6211 \ Users \ u5728 \ Users \ u6765 \ Users \ u8fd9 \ Users \ u7740 \ u4e2a \ u5730 \ u5230 \ u5927 \ u91cc \ u8bf4 \ Alibaba \ u53bb \ Alibaba \ u90a3 \ u8981 \ Alibaba \ u65f6 \ u8fc7 \ u51fa \ u5c0f \ u4e48 \ u8d77 \ Users \ u90fd \ u628a \ u597d \ u8fd8 \ u591a \ u6ca1 \ u4e3a \ u53c8 \ u53ef \ u5bb6 \ Users \ u53ea \ Users \ u6837 \ Users \ u60f3 \ Users \ Users \ u8001 \ Users \ u4ece \ u81ea \ u9762 \ u524d \ u5934 \ u9053 \ u5b83 \ u540e \ Users \ u50cf \ u89c1 \ Users \ u7528 \ u5979 \ u56fd \ u52a8 \ u8fdb \ u6210 \ u56de \ Alibaba \ u800c \ Alibaba \ u5c71 \ u6c11 \ u5019 \ u7ecf \ u53d1 \ u5de5 \ u5411 \ u4e8b \ u547d \ u7ed9 \ Users \ u4e09 \ u58f0 \ Users \ u773c \ u5fd7 \ u70b9 \ u5fc3 \ u6218 \ Users \ u95ee \ u4f46 \ u8eab \ u65b9 \ Users \ u5403 \ u505a \ u53eb \ Users \ u542c \ u9769 \ Users \ u5462 \ Users \ u5168 \ Users \ u56db \ Users \ u6240 \ Users \ u6700 \ u5149 \ u4ea7 \ u60c5 \ u8def \ u5206 \ u603b \ u6761 \ u767d \ Users \ u5982 \ u88ab \ u82b1 \ u53e3 \ u653e \ u513f \ u5e38 \ Alibaba \ u7b2c \ Alibaba \ u5427 \ u6587 \ Alibaba \ u600e \ u5b9a \ u8bb8 \ u5feb \ u660 \ u884c \ Users \ u98de \ u5916 \ u6811 \ u00009 \ u6d3b \ u90e8 \ u95e8 \ u65e0 \ u5f80 \ u8239 \ u671b \ Users \ Users \ u5458 \ u673a \ u66f4 \ u4e5d \ u60a8 \ u6bcf \ u98ce \ u7ea7 \ u8ddf \ u7b11 \ Users \ u76f4 \ Users \ u591c \ Users \ u9636 \ u8fde \ u8f66 \ u91cd \ u4fbf \ u6597 \ Users \ u54ea \ u5316 \ u592a \ Users \ u793e \ Users \ u58eb \ u8005 \ Users \ u77f3 \ u65e5 \ u51b3 \ u767e \ Users \ u62ff \ Users \ u672c \ u601d \ u89e3 \ Users \ u6751 \ Users \ u96be \ u65e9 \ u8bba \ u5417 \ u6839 \ u5171 \ u8ba9 \ u76f8 \ u7814 \ u4eca \ Alibaba \ u5750 \ u63a5 \ Alibaba \ u89c9 \ Alibaba \ u53cd \ Alibaba \ u8bb0 \ u5c06 \ Alibaba \ Users \ u4e89 \ u9886 \ u6216 \ Users \ u5757 \ Users \ u52a0 \ u811a \ u7d27 \ u7231 \ u7b49 \ u4e60 \ u9635 \ u6015 \ Users \ u9752 \ u534a \ u706b \ u6cd5 \ u9898 \ u5efa \ u8d76 \ u4f4d \ u5531 \ Users \ u5973 \ Users \ u51c6 \ Users \ u56e2 \ u79bb \ u8272 \ Users \ u79d1 \ u5012 \ u775b \ u5229 \ u4e16 \ u521a \ u4e14 \ u7531 \ u9001 \ Users \ u8868 \ Users \ u6574 \ Users \ u54cd \ u96ea \ u6d41 \ u672a \ u573a \ u8be5 \ u5e76 \ u5e95 \ u6df1 \ u523b \ Users \ u4family \ Users \ Users \ u53e4 \ Users \ u544a \ Users \ u62c9 \ u540d \ u5440 \ u571f \ u6e05 \ u9633 \ Users \ u53f2 \ u6539 \ Users \ u753b \ u9020 \ u5634 \ u6b64 \ u6cbb \ u5317 \ u5fc5 \ Alibaba \ u83dc \ u722c \ u7761 \ u5174 \ u5f62 \ u91cf \ u54b1 \ U \ u82e6 \ Users \ u901a \ u51b2 \ u5408 \ u7834 \ u53cb \ u5ea6 \ u672f \ u996d \ u516c \ u65c1 \ Users \ u6781 \ Users \ u67aa \ Users \ Alibaba \ u91ce \ u575a \ Alibaba \ u6536 \ Alibaba \ u81f3 \ Alibaba \ u57ce \ u52b3 \ u843d \ u94b1 \ Alibaba \ u56f4 \ Alibaba \ u80dc \ u6559 \ u70ed \ u5c55 \ u5305 \ u6b4c \ u7c7b \ u6e10 \ Alibaba \ u6570 \ Alibaba \ u6027 \ u97f3 \ Alibaba \ u9645 \ u65e7 \ Alibaba \ u5e2e \ u5566 \ u53d7 \ u7cfb \ Users \ u8df3 \ u975e \ u4f55 \ u725b \ u53d6 \ Users \ u6133 \ Users \ u79cd \ u88c5 \ u9876 \ u6025 \ u6797 \ u505c \ Users \ u8863 \ u822c \ u62a5 \ u53f6 \ u538b \ u6162 \ u53d4 \ u80cc \ u7ec6 ";
Int x = 5; // set the position where Chinese characters appear
For (INT I = 0; I <4; I ++ ){
// Set the rotation radians of a word, which must not exceed 30 degrees
// Int degree = new random (). nextint (30); // This design rotates only in one direction
// The Random Number % 30 must be smaller than 30. This ensures that the rotation radians of Chinese characters are between-30 and ~ Between 30
Int degree = new random (). nextint () % 30;
// Convert the obtained characters into strings
String CH = base. charat (new random (). nextint (base. Length () + "";
// Set the Rotation Angle
G. Rotate (degree * Math. PI/180, X, 20 );
// Fill Chinese Characters
G. drawstring (CH, X, 20 );
// After rotation, the rotation operation must be restored to avoid affecting the next operation.
G. Rotate (-degree * Math. PI/180, X, 20 );
X + = 30; // set the position where the next word appears
}
}
Private void drawrandomline (Graphics g ){
// Set the color of the interference line to green.
G. setcolor (color. Green );
// Draw 5 interference lines
For (INT I = 0; I <5; I ++ ){
// Start coordinate of the interference line
Int X1 = new random (). nextint (width );
Int Y1 = new random (). nextint (height );
// Coordinates of the end of the interference line
Int X2 = new random (). nextint (width );
Int y2 = new random (). nextint (height );
// Draw interference lines
G. drawline (x1, Y1, X2, Y2 );
}
}
Private void setborder (Graphics g ){
// Set the border color to blue
G. setcolor (color. Blue );
// Fill (if it starts from 0, the border will be painted outside)
G. drawrect (1, 1, width-2, height-2 );
}
Private void setbackground (Graphics g ){
// Set the background color to white
G. setcolor (color. White );
// Fill the rectangle
G. fillrect (0, 0, width, height );
}
Protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
Doget (request, response );
}
}
/*************************************** ***************************** **************************/
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> User Registration </title>
<SCRIPT type = "text/JavaScript">
Function changeimg (IMG ){
IMG. src = IMG. SRC + "? "+ New date (). gettime ();
}
</SCRIPT>
</Head>
<Body>
<Form>
Username: <input type = "text" name = "user"/> <br/>
Password: <input type = "password" name = "PWD"/> <br/>
Verification Code: <input type = "text" name = "check"/>
<! -- Click the image verification code to change the image -->
<br/>
<Input type = "Submit" value = "Submit">
</Form>
</Body>
</Html>