Generates four verification codes containing letters, numbers, and numbers.

Source: Internet
Author: User
1 package sanitation. util;
2
3 Import javax. servlet .*;
4 Import javax. servlet. http .*;
5 import java. Io .*;
6 Import java. AWT .*;
7 Import java. AWT. image .*;
8 Import java. util .*;
9 Import javax. ImageIO .*;
10
11 public class authimg extends httpservlet
12 {
13 /**
14 * processing of verification code Images
15 */
16 Private Static final long serialversionuid = 1l;
17 private font mfont = new font ("Arial black", Font. Plain, 16); // create a new font Based on the specified name, style, and pounds
18 public void Init () throws servletexception
19 {
20 super. INIT ();
21}
22 // randomly generate the color of the specified interval
23 color getrandcolor (int fc, int BC)
24 {
25 random = new random ();
26 if (FC> 255) fc = 255;
27 if (BC> 255) BC = 255;
28 int r = FC + random. nextint (BC-Fc );
29 int G = FC + random. nextint (BC-Fc );
30 int B = FC + random. nextint (BC-Fc );
31 return new color (R, G, B );
32}
33
34 public void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception
35 {
36 response. setheader ("Pragma", "No-Cache ");
37 Response. setheader ("cache-control", "No-Cache ");
38 response. setdateheader ("expires", 0 );
39 response. setcontenttype ("image/JPEG ");
40 system. Out. println ("I generated ");
41 int width = 80, Height = 18;
42 /**
43 * construct a bufferedimage with one of the predefined image types. The three parameters are as follows: A. Width of the created image
44 * B. Height of the created image C. Type of the created image
45 **/
46 bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
47 // create a graphic Context
48 graphics G = image. getgraphics ();
49 random = new random ();
50 // set the current color of the image context to the specified color
51g. setcolor (getrandcolor (200,250 ));
52 // fill in the specified rectangle, fillrect (int x, int y, int width, int height). The left and right sides of the rectangle are located in X and x + width-1, respectively. The top and bottom edges are located at Y and Y + height-1, width and height respectively.
53G. fillrect (1, 1, width-1, height-1 );
54g. setcolor (new color (102,102,102 ));
55 // draw the border of the specified rectangle.
56G. drawrect (0, 0, width-1, height-1 );
57 // set the font of the image context to the specified font
58G. setfont (mfont );
59G. setcolor (getrandcolor (160,200 ));
60 for (INT I = 0; I <155; I ++)
61 {
62 int x = random. nextint (width-1 );
63 int y = random. nextint (height-1 );
64 int XL = random. nextint (6) + 1;
65 int yl = random. nextint (12) + 1;
66 // drawline (INT X1, int Y1, int X2, int Y2) in the coordinate system of the image context, the current color is used at the points (x1, Y1) and (X2, Y2) draw a line between them.
67g. drawline (X, Y, x + XL, Y + yl );
68}
69 for (INT I = 0; I <70; I ++)
70 {
71 int x = random. nextint (width-1 );
72 int y = random. nextint (height-1 );
73 int XL = random. nextint (12) + 1;
74 int yl = random. nextint (6) + 1;
75g. drawline (X, Y, X-XL, Y-yl );
76}
77 /**
Below 78 * are four random numbers (including uppercase letters, lowercase letters, and numbers)
79 **/
80 string srand = "";
81 For (INT I = 0; I <4; I ++)
82 {
83 // returns a string
84 string TMP = getrandomchar ();
85 // The following process is performed to prevent ambiguity between 0 and O.
86 If (TMP. Equals ("O") | TMP. Equals ("O ")){
87 TMP = "";
88}
89 If (TMP = "0 "){
90 TMP = "B ";
91}
92 srand + = TMP;
93G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110 )));
94 // use the current font and color of the image context to draw the text given by the specified string. The baseline of the leftmost character is located at the (x, y) Position of the graph context Coordinate System
95g. drawstring (TMP, 15 * I + 10, 15 );
96}
97 system. Out. println ("generated verification code" + srand. touppercase ());
98
99 httpsession session = request. getsession (true );
100 session. setattribute ("RAND", srand );
101 // release the context of the image and all system resources it uses
102g. Dispose ();
103 /**
104 * after the image is created, an image is written to outputstream using ImageIO. Write (renderedimage im, string formatname, outputstream output ).
105 **/
106 ImageIO. Write (image, "Jpeg", response. getoutputstream ());
107}
108 /**
109 * obtain uppercase letters, lowercase letters, or numbers.
110 **/
111 private string getrandomchar ()
112 {
113 // generate a random number less than 2 and perform rounding
114 int Rand = (INT) math. Round (math. Random () * 2 );
115 long itmp = 0;
116 char CTMP = '\ u0000 ';
117 switch (RAND)
118 {
119 Case 1:
120 // generate-90 ASCII
121 itmp = math. Round (math. Random () * 25 + 65 );
122 // forcibly convert to Char and obtain the corresponding uppercase letters
123 CTMP = (char) itmp;
124 // The break can be omitted if return is provided below
125 return string. valueof (CTMP );
126 Case 2:
127 // get the ASCII of 97-122 and lowercase letters
128 itmp = math. Round (math. Random () * 25 + 97 );
129 CTMP = (char) itmp;
130 return string. valueof (CTMP );
Default 131:
132 // obtain a number
133 itmp = math. Round (math. Random () * 9 );
134 return string. valueof (itmp );
135}
136}
137}
}

Because the project requires the user to enter a verification code during login to verify the login, so you can find the relevant information to implement the verification code,

The above S is the four verification codes that contain uppercase and lowercase letters and numbers implemented by Servlet:

The following figure shows the general servlet configuration in the web. xml configuration file.

    <servlet>
<servlet-name>img</servlet-name>
<servlet-class>sanitation.Util.AuthImg</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>img</servlet-name>
<url-pattern>/authImg</url-pattern>
</servlet-mapping>

This completes. You only need to reference the servlet on the page: Sample Code

 <a href = "#" onclick = "Refresh ()"> cannot see clearly, change one </a>

Haha, I haven't written a blog for a long time. It's pretty cool to write.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.