1 Package Net. cmono. javaweb. C3;
2
3 Import Java. AWT. color;
4 Import Java. AWT. Font;
5 Import Java. AWT. graphics2d;
6 Import Java. AWT. image. bufferedimage;
7 Import Java. Io. ioexception;
8 Import Java. Io. printwriter;
9 Import Java. util. Random;
10
11 Import Javax. servlet. servletexception;
12 Import Javax. servlet. servletoutputstream;
13 Import Javax. servlet. http. httpservlet;
14 Import Javax. servlet. http. httpservletrequest;
15 Import Javax. servlet. http. httpservletresponse;
16
17 Import Com. Sun. CORBA. se. impl. ior. newobjectkeytemplatebase;
18 Import Com.sun.image.codec.jpeg. Unzip codec;
19 Import Com.sun.image.codec.jpeg. Specify imageencoder;
20
21 Public Class Identityservlet Extends Httpservlet {
22
23 /**
24 * Constructor of the object.
25 */
26 Public Identityservlet (){
27 Super ();
28 }
29
30 /**
31 * Destruction of the servlet. <br>
32 */
33 Public Void Destroy (){
34 Super . Destroy (); // Just puts "Destroy" string in log
35 // Put your code here
36 }
37
38 /**
39 * The doget method of the servlet. <br>
40 *
41 * This method is called when a form has its tag Value Method equals to get.
42 *
43 * @ Param Request the request send by the client to the server
44 * @ Param Response the response send by the server to the client
45 * @ Throws Servletexception if an error occurred
46 * @ Throws Ioexception if an error occurred
47 */
48 Public Void Doget (httpservletrequest request, httpservletresponse response)
49 Throws Servletexception, ioexception {
50
51 Response. setcontenttype ("image/JPEG ");
52
53 String randomstring = getrandomstring ();
54 Request. getsession ( True ). Setattribute ("randomstring", randomstring );
55
56 Int Length = 100;
57 Int Height = 30;
58
59 Color color = getrandomcolor ();
60 Color reverse = getreversecolor (color );
61
62 Bufferedimage Bi = New Bufferedimage (width, height, bufferedimage. type_int_rgb );
63
64 Graphics2d G = Bi. creategraphics ();
65 G. setfont ( New Font (font. sans_serif, Font. Bold, 16 ));
66 G. setcolor (color );
67 G. fillrect (0, 0, width, height );
68 G. setcolor (reverse );
69 G. drawstring (randomstring, 18, 20 );
70
71 For ( Int I = 0, n = random. nextint (100); I <n; I ++)
72 {
73 G. drawrect (random. nextint (width), random. nextint (height), 1, 1 );
74 }
75
76 Servletoutputstream out = response. getoutputstream ();
77
78 Required imageencoder encoder = required codec. createjpegencoder (out );
79
80 Encoder. encode (BI );
81
82 Out. Flush ();
83
84 }
85
86 /**
87 * The dopost method of the servlet. <br>
88 *
89 * This method is called when a form has its tag Value Method equals to post.
90 *
91 * @ Param Request the request send by the client to the server
92 * @ Param Response the response send by the server to the client
93 * @ Throws Servletexception if an error occurred
94 * @ Throws Ioexception if an error occurred
95 */
96 Public Void Dopost (httpservletrequest request, httpservletresponse response)
97 Throws Servletexception, ioexception {
98
99 }
100
101 /**
102 * Initialization of the servlet. <br>
103 *
104 * @ Throws Servletexception if an error occurs
105 */
106 Public Void Init () Throws Servletexception {
107 // Put your code here
108 }
109
110 Public Static Final Char [] Chars = {'2', '3', '4', '5', '6', '7', '8', '9 ', 'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'J', 'k ',
111 'L', 'M', 'n', 'P', 'Q', 'R', 's', 't', 'U', 'V ', 'w', 'x', 'y', 'z '};
112
113 Public Static Random random = New Random ();
114
115 Public Static String getrandomstring ()
116 {
117 Stringbuffer buffer = New Stringbuffer ();
118 For ( Int I = 0; I <6; I ++)
119 {
120 Buffer. append (chars [random. nextint (chars. Length)]);
121 }
122
123 Return Buffer. tostring ();
124 }
125
126 Public Static Color getrandomcolor ()
127 {
128 Return New Color (random. nextint (255), random. nextint (255), random. nextint (255 ));
129 }
130
131 Public Static Color getreversecolor (color C)
132 {
133 Return New Color (255-C. getred (), 255-C. getgreen (), 255-c.getblue ());
134 }
135
136 }
1 < Script Type = "Text/JavaScript" >
2 Function Reloadimage ()
3 {
4 Document. getelementbyid ( ' BTN ' ). Disabled = True ;
5 Document. getelementbyid ( ' Identity ' ). SRC = ' Servlet/indentityservlet? TS ' + New Date (). gettime ();
6 }
7 </ Script >
8
9 < IMG SRC = "Servlet/identityservlet" ID = "Identity" Onload = "BTN. Disabled = false ;"
10
11 <Input type = "Button" Value = "Change image" Onclick = "Reloadimage ()" ID = "BTN" />