Response Summary (III)-output random image (Verification Code Implementation)

Source: Internet
Author: User

This article is a simple implementation of the verification code output to the web page! Each step is explained through annotations!

The file structure of package explorer in myeclipse is as follows:

Responsedemo. Java implements the function of outputting random images:

1 package COM. yyz. response; 2 3 Import Java. AWT. color; 4 Import Java. AWT. font; 5 import Java. AWT. graphics; 6 Import Java. AWT. image. bufferedimage; 7 Import Java. io. ioexception; 8 Import Java. util. random; 9 10 Import javax. imageIO. imageIO; 11 import javax. servlet. servletexception; 12 Import javax. servlet. HTTP. httpservlet; 13 Import javax. servlet. HTTP. httpservletrequest; 14 Import javax. servlet. HTTP. httpservletresponse; 15 // output random image 16 public class responsedemo extends httpservlet {17 18 public void doget (httpservletrequest request, httpservletresponse response) 19 throws servletexception, ioexception {20/6. set the header and control the browser not to cache image data 21 response. setheader ("expires", "-1"); 22 response. setheader ("cache-control", "No-Cache"); 23 response. setheader ("Pragma", "No-Cache"); 24 25 // 5. notify the browser to open 26 response as an image. setheader ("Content-Type", "image/JPEG"); 27 // 1. create a secondary image in the memory 28 bufferedimage image = new bufferedimage (, bufferedimage. type_int_rgb); 29 // 2. obtain the image 30 graphics G = image. getgraphics (); 31 // modify the 32G background color of the image. setcolor (color. white); 33g. fillrect (0, 0, 80, 20); 34 // 3. write Data 35 GB to the image. setcolor (color. blue); 36g. setfont (new font (null, Font. bold, 20); 37g. drawstring (nakenum (), 0, 20); 38 // randomly draw 9 interference lines 39 random Rand = new random (); 40 for (INT I = 0; I <9; I ++) {41g. setcolor (color. black); 42 int X1 = Rand. nextint (80); 43 int Y1 = Rand. nextint (20); 44 int X2 = Rand. nextint (80); 45 int y2 = Rand. nextint (20); 46G. drawline (x1, Y1, X2, Y2); 47} 48 // 4. write the image to the browser 49 ImageIO. write (image, "Jpeg", response. getoutputstream (); 50 51} 52 53 private string nakenum () {54 random r = new random (); 55 string num = R. nextint (9999999) + ""; // generates numbers ranging from 0 to 9999999, but there may be less than 7 digits, such as 12356 stringbuffer sb = new stringbuffer (); 57 for (INT I = 0; I <7-num.length (); I ++) {58 sb. append ("0"); 59} 60 return sb. tostring () + num; 61} 62 63 public void dopost (httpservletrequest request, httpservletresponse response) 64 throws servletexception, ioexception {65 doget (request, response); 66} 67}

Register.html is a simple registration page that uses the IMG label to execute responsedemo. Java:

1 <! Doctype HTML> 2 <HTML> 3 

Finally, the test results are attached:

When outputting a random image, If You Want To output Chinese characters on the image, you can use a single character array to store multiple Chinese characters (the encoding of Chinese characters can also be saved directly ), then, use the random number generated by random as the subscript to retrieve the corresponding Chinese character, and then generate the string to be written.

 

 

 

 

 

Response Summary (III)-output random image (Verification Code Implementation)

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.