Programming from scratch for cainiao-simple verification code generation (Java version)

Source: Internet
Author: User

We all know the verification code. I don't need to say much about its role. If you are not quite clear, refer to the explanation in Baidu encyclopedia. Generally, the generation of verification codes generates random characters (numbers, letters, or Chinese characters), and then draws the generated characters into an image, add some interference elements to the image, such as various lines. Well, let's look at it together:

Simple logon page (the code is incomplete, only the verification code)
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> 

Use session. getattribute ("RAND") to obtain the generated verification code, compare it with the user input, and then process the result accordingly.

Generate a verification code using Servlet:

Package COM. util. servlet; import Java. AWT. color; import Java. AWT. font; import Java. AWT. graphics; 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; import javax. servlet. HTTP. httpsession; public class authimageservlet extends httpservlet {Private Static final string content_type = "text/html; charset = gb2312"; // set the letter size, size private font mfont = new font ("Times New Roman", Font. plain, 17); Public void Init () throws servletexception {super. init ();} color getrandcolor (int fc, int BC) {random = new random (); If (FC> 255) fc = 255; If (BC> 255) BC = 255; int r = FC + random. nextint (BC-Fc); int G = FC + random. nextint (BC-Fc); int B = FC + random. nextint (BC-Fc); return new color (R, G, B);} public void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setheader ("Pragma", "No-Cache"); response. setheader ("cache-control", "No-Cache"); response. setdateheader ("expires", 0); // indicates that the generated response is image response. setcontenttype ("image/JPEG"); int width = 100, Height = 18; bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb); graphics G = image. getgraphics (); random = new random (); G. setcolor (getrandcolor (200,250); G. fillrect (1, 1, width-1, height-1); G. setcolor (new color (102,102,102); G. drawrect (0, 0, width-1, height-1); G. setfont (mfont); G. setcolor (getrandcolor (160,200); // draw a random line for (INT I = 0; I <155; I ++) {int x = random. nextint (width-1); int y = random. nextint (height-1); int XL = random. nextint (6) + 1; int yl = random. nextint (12) + 1; G. drawline (X, Y, x + XL, Y + yl);} // draw a random line from the other direction for (INT I = 0; I <70; I ++) {int x = random. nextint (width-1); int y = random. nextint (height-1); int XL = random. nextint (12) + 1; int yl = random. nextint (6) + 1; G. drawline (X, Y, X-XL, Y-yl);} // generates a random number and converts it to string srand = ""; for (INT I = 0; I <6; I ++) {int itmp = random. nextint (26) + 65; char CTMP = (char) itmp; srand + = string. valueof (CTMP); G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110); G. drawstring (string. valueof (CTMP), 15 * I + 10, 16);} httpsession session = request. getsession (true); Session. setattribute ("RAND", srand); G. dispose (); ImageIO. write (image, "Jpeg", response. getoutputstream ());}}

Servlet configuration information in Web. xml:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"     xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <servlet>    <servlet-name>AuthImageServlet</servlet-name>    <servlet-class>com.util.servlet.AuthImageServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>AuthImageServlet</servlet-name>    <url-pattern>/servlet/AuthImageServlet</url-pattern>  </servlet-mapping>    <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

In this way, the verification code function is implemented, which is very simple and practical. Of course, there are more beautiful and more rigorous methods. If you are interested, you can study them in depth. Here is a simple method to share with you. Thank you for your criticism.


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.