1. Use servlet-only verification code
(1) Configure in Web. xml:
<Servlet> <br/> <servlet-Name> image </servlet-Name> <br/> <servlet-class> Org. test. web. authimage </servlet-class> <br/> </servlet> </P> <p> <servlet-mapping> <br/> <servlet-Name> image </Servlet -Name> <br/> <URL-pattern>/authimage </url-pattern> <br/> </servlet-mapping>
(2) Servlet Source Code
Public class authimage extends httpservlet <br/>{</P> <p> Private Static final string content_type = "text/html; charset = gb2312 "; <br/> // set the letter size. <br/> private font mfont = new font ("Times New Roman", Font. plain, 17); <br/> Public void Init () throws servletexception <br/>{< br/> super. init (); <br/>}< br/> color getrandcolor (int fc, int BC) <br/>{< br/> random = new random (); <br/> If (FC> 255) fc = 255; <br/> If (BC> 255) BC = 255; <br/> int r = FC + random. nextint (BC-Fc); <br/> int G = FC + random. nextint (BC-Fc); <br/> int B = FC + random. nextint (BC-Fc); <br/> return new color (R, G, B ); <br/>}</P> <p> Public void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception <br/>{< br/> response. setheader ("Pragma", "No-Cache"); <br/> response. setheader ("cache-control", "No-Cache"); <br/> response. setdateheader ("expires", 0); <br/> // indicates that the generated response is an image <br/> response. setcontenttype ("image/JPEG"); </P> <p> int width = 100, Height = 18; <br/> bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb); </P> <p> graphics G = image. getgraphics (); <br/> random = new random (); <br/> G. setcolor (getrandcolor (200,250); <br/> G. fillrect (1, 1, width-1, height-1); <br/> G. setcolor (new color (102,102,102); <br/> G. drawrect (0, 0, width-1, height-1); <br/> G. setfont (mfont); </P> <p> G. setcolor (getrandcolor (160,200); </P> <p> // draw a random line <br/> for (INT I = 0; I <155; I ++) <br/>{< br/> int x = random. nextint (width-1); <br/> int y = random. nextint (height-1); <br/> int XL = random. nextint (6) + 1; <br/> int yl = random. nextint (12) + 1; <br/> G. drawline (X, Y, x + XL, Y + yl ); <br/>}</P> <p> // draw a random line from the other direction <br/> for (INT I = 0; I <70; I ++) <br/>{< br/> int x = random. nextint (width-1); <br/> int y = random. nextint (height-1); <br/> int XL = random. nextint (12) + 1; <br/> int yl = random. nextint (6) + 1; <br/> G. drawline (X, Y, X-XL, Y-yl); <br/>}</P> <p> // generates a random number, and convert random numbers to letters <br/> string srand = ""; <br/> for (INT I = 0; I <6; I ++) <br/>{< br/> int itmp = random. nextint (26) + 65; <br/> char CTMP = (char) itmp; <br/> srand + = string. valueof (CTMP); <br/> G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110); <br/> G. drawstring (string. valueof (CTMP), 15 * I + 10, 16); <br/>}</P> <p> httpsession session = request. getsession (true); <br/> session. setattribute ("RAND", srand); <br/> G. dispose (); <br/> ImageIO. write (image, "Jpeg", response. getoutputstream (); <br/>}< br/> Public void destroy () <br/>{< br/>}< br/>}
(3) page display
Ii. use JSP to implement verification code
<% @ Page Language = "Java" Import = "Java. AWT. *, Java. AWT. image. *, Java. util. *, javax. imageIO. * "<br/> contenttype =" image/JPEG "pageencoding =" UTF-8 "%> </P> <p> <% // set page not to cache <br/> response. setheader ("Pragma", "No-Cache"); <br/> response. setheader ("cahce-control", "No-Cache"); <br/> response. setdateheader ("expires", 0); <br/> // create an image in the memory <br/> int width = 60, Height = 20; <br/> bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb); <br/> // obtain the image context <br/> graphics G = image. getgraphics (); <br/> // generate a random class <br/> random = new random (); <br/> // set the background color <br/> G. setcolor (new color (160,200,100); <br/> G. fillrect (0, 0, width, height); <br/> // set the font <br/> G. setfont (new font ("Times New Roman", Font. plain, 18); <br/> // generates 50 random interference lines, making the verification code in the image hard to be detected by other programs. <br/> G. setcolor (new color (160,200,200); <br/> for (INT I = 0; I <50; I ++) <br/>{< br/> int x = random. nextint (width); <br/> int y = random. nextint (height); <br/> int X1 = random. nextint (width); <br/> int Y1 = random. nextint (height); <br/> G. drawline (X, Y, x + X1, Y + Y1); <br/>}< br/> // A Random verification code (6 digits) <br/> string srand = ""; <br/> for (INT I = 0; I <6; I ++) <br/>{< br/> string Rand = string. valueof (random. nextint (10); <br/> srand + = rand; <br/> // display the verification code to the image <br/> G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110); <br/> G. drawstring (RAND, 13 * I + 6, 16); <br/>}< br/> session. setattribute ("RAND", srand); // store the generated verification code to sesson. <br/> G. dispose (); <br/> ImageIO. write (image, "Jpeg", response. getoutputstream (); <br/> out. clear (); // ************ <br/> out = pagecontext. pushbody (); // *********** <br/> %> <br/>
3. Use struts2 for verification code
(1) define a tool class for generating verification Codes
Package com.cn. hospital. util; </P> <p> Import Java. AWT. color; <br/> Import Java. AWT. font; <br/> Import Java. AWT. graphics; <br/> Import Java. AWT. image. bufferedimage; <br/> Import Java. io. bytearrayinputstream; <br/> Import Java. io. bytearrayoutputstream; <br/> Import Java. util. random; <br/> Import javax. imageIO. imageIO; <br/> Import javax. imageIO. stream. imageoutputstream; </P> <p> public class randomnumutil {</P> <p> private bytearrayinputstream image; // image <br/> private string STR; // Verification Code </P> <p> private randomnumutil () {<br/> Init (); // initialization attribute <br/>}< br/>/* <br/> * obtain the randomnumutil instance <br/> */<br/> Public static randomnumutil instance () {<br/> return New randomnumutil (); <br/>}< br/>/* <br/> * obtain the verification code image <br/> */<br/> Public bytearrayinputstream getimage () {<br/> return this. image; <br/>}< br/>/* <br/> * obtain the image Verification Code <br/> */<br/> Public String getstring () {<br/> return this. STR; <br/>}</P> <p> private void Init () {<br/> // create an image in the memory <br/> int width = 85, height = 20; <br/> bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb); <br/> // obtain the image context <br/> graphics G = image. getgraphics (); <br/> // generate a random class <br/> random = new random (); <br/> // set the background color <br/> G. setcolor (getrandcolor (200,250); <br/> G. fillrect (0, 0, width, height); <br/> // set the font <br/> G. setfont (new font ("Times New Roman", Font. plain, 18); <br/> // generates 155 random interference lines, making the authentication code in the image hard to be detected by other programs. <br/> G. setcolor (getrandcolor (160,200); <br/> for (INT I = 0; I <155; I ++) <br/>{< br/> int x = random. nextint (width); <br/> int y = random. nextint (height); <br/> int XL = random. nextint (12); <br/> int yl = random. nextint (12); <br/> G. drawline (X, Y, x + XL, Y + yl); <br/>}< br/> // obtain the random ID code (6 digits) <br/> string srand = ""; <br/> for (INT I = 0; I <6; I ++) {<br/> string Rand = string. valueof (random. nextint (10); <br/> srand + = rand; <br/> // display the authentication code in the image <br/> G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110); <br/> // The color from the call function is the same, probably because the seed is too close, so it can only be directly generated <br/> G. drawstring (RAND, 13 * I + 6, 16); <br/>}< br/> // value Verification Code <br/> This. STR = srand; </P> <p> // image effective <br/> G. dispose (); <br/> bytearrayinputstream input = NULL; <br/> bytearrayoutputstream output = new bytearrayoutputstream (); <br/> try {<br/> imageoutputstream imageout = ImageIO. createimageoutputstream (output); <br/> ImageIO. write (image, "Jpeg", imageout); <br/> imageout. close (); <br/> input = new bytearrayinputstream (output. tobytearray (); <br/>}catch (exception e) {<br/> system. out. println ("Incorrect verification code Image Generation:" + E. tostring (); <br/>}</P> <p> This. image = input; /* assign an image */<br/>}< br/>/* <br/> * obtain a random color in a given range <br/> */<br/> private color getrandcolor (int fc, int BC) {<br/> random = new random (); <br/> If (FC> 255) fc = 255; <br/> If (BC> 255) BC = 255; <br/> int r = FC + random. nextint (BC-Fc); <br/> int G = FC + random. nextint (BC-Fc); <br/> int B = FC + random. nextint (BC-Fc); <br/> return new color (R, G, B); <br/>}< br/>
(2) define the Action output by a verification code
Package com.cn. hospital. action; </P> <p> Import Java. io. bytearrayinputstream; <br/> Import Java. io. bytearrayoutputstream; </P> <p> Import Org. springframework. context. annotation. scope; <br/> Import Org. springframework. stereotype. controller; </P> <p> Import com.cn. hospital. util. randomcharutil; <br/> Import com.cn. hospital. util. randomnumutil; <br/> Import COM. opensymphony. xwork2.actioncontext; <br/> Import COM. opensymphony. xwork2.actionsupport; </P> <p> @ controller ("utilaction") <br/> @ scope ("prototype ") <br/> public class utilaction extends actionsupport {</P> <p> Private Static final long serialversionuid =-7193209177116825032l; <br/> private bytearrayinputstream inputstream; </P> <p> private int width; <br/> private int height; <br/> private int fontsize; <br/> private int codelength; <br/> private int disturbtype; </P> <p> Public String validnumgenerate () throws exception {<br/> randomnumutil rdnu = randomnumutil. instance (); <br/> This. setinputstream (rdnu. getimage (); // obtain an image with a random string <br/> actioncontext. getcontext (). getsession (). put ("random", rdnu. getstring (); // get the random string and put it into httpsession <br/> return success; <br/>}</P> <p> Public void setinputstream (bytearrayinputstream inputstream) {<br/> This. inputstream = inputstream; <br/>}</P> <p> Public bytearrayinputstream getinputstream () {<br/> return inputstream; <br/>}</P> <p >}< br/>
(3) struts. xml configuration
<! -- Generate a Random verification code --> <br/> <action name = "randnum" class = "utilaction" method = "validnumgenerate"> <br/> <result name = "success" Type = "stream"> <br/> <Param name = "contenttype"> image/JPEG </param> <br/> <Param name = "inputname"> inputstream </Param> <br/> </result> <br/> </Action>
Iv. Summary
For Java Web technology, servlet is executed on the server. From the three different implementations above, we can easily see that they have one thing in common, that is, return to the browser-side contenttype.
Servlet: implemented using the response. setcontenttype (""); Method
JSP: Implemented in <@ page contenttype = "">
Struts2: Implemented by configuring <Param name = "contenttype"> </param>
As for the generation of verification codes, it is actually relatively simple, so we will not go into it here. In this regard, I am a little bit impressed with my peers.