Most people may not understand the verification code function, but almost every security website has it. Verification codes are used to prevent non-human operations. For example, if a hacker wants to hack a website, how can this problem be solved? The simplest idea is of course to cause network congestion until the system crashes. If there is no verification code, then I can write a program on the registration page, only the registry list, constantly changing the primary key or non-repeated content, and constantly submitting. In this way, it is possible to register tens of thousands of times every second, so that the server will load a lot and it will easily crash and die. It is not difficult to achieve this purpose. After the verification code is added, it is not the robot that can recognize anything else. It must be a person in the operating system, because it is in the form of pictures, and it is messy, at present, the graphic pattern recognition technology has not been able to identify the verification code, so it is much safer to add the verification code. In software development, it is inevitable that customers do not know what the verification code is. Why? Because everyone has it. There is no way to add it to appear professional. In fact, this is easy to add. It is to use a number or letter as a template and use the swing graphic API to draw an image. The complete logic and Code are as follows: [html] [javascript] // refresh the verification code flushValidateCode = function (obj) {obj. src = 'validatecodeservlet? D = '+ new Date ();} The request URL is validateCodeServlet. The following parameter d = new Date () ensures that the URL is different each time you click to refresh. Otherwise, the request will not be refreshed. You can also use Math. rand (); to obtain a random number. Web. xml to configure the servlet ing information. [Html] <servlet> <servlet-name> validateCode </servlet-name> <servlet-class> com. xzfy. mainpage. web. action. validateCodeServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> validateCode </servlet-name> <url-pattern>/validateCodeServlet </url-pattern> </servlet-mapping> ValidateCodeServlet [java] package com. xzfy. mainpage. web. action; import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. image. bufferedImage; import java. io. IOException; import java. io. printWriter; import java. util. random; import javax. imageio. imageIO; import javax. servlet. servletException; import javax. servlet. servletOutputStream; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpSession; import org. apache. commons. io. output. byteArrayOutputStream; public class ValidateCodeServlet extends HttpServlet {/*** Constructor of the object. */public ValidateCodeServlet () {super ();}/*** Destruction of the servlet. <br> */public void destroy () {super. destroy (); // Just puts "destroy" string in log // Put your code here}/*** The doGet method of the servlet. <br> ** This method is called when a form has its tag value method equals to get. ** @ param request the request send by the client to the server * @ param response the response send by the server to the client * @ throws ServletException if an error occurred * @ throws IOException if an error occurred */public void doGet (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {response. setContentType ("image/jpeg"); response. setHeader ("Pragma", "No-cache"); response. setHeader ("Cache-Control", "no-cache"); response. setDateHeader ("Expires", 0); HttpSession session = request. getSession (); int width = 60, height = 20; BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); Graphics g = image. getGraphics (); Random random = new Random (); g. setColor (getRandColor (200,250); g. fillRect (0, 0, width, height); g. setFont (new Font ("Times New Roman", Font. PLAIN, 18); g. setColor (getRandColor (160,200); for (int I = 0; I <155; I ++) {int x = random. nextInt (width); int y = random. nextInt (height); int xl = random. nextInt (12); int yl = random. nextInt (12); g. drawLine (x, y, x + xl, y + yl);} String sRand = ""; for (int I = 0; I <4; I ++) {String rand = String. valueOf (random. nextInt (10); sRand + = rand; g. setColor (new Color (20 + random. nextInt (110), 20 + random. nextInt (110), 20 + random. nextInt (110); // The color from the call function is the same, probably because the seed is too close, so you can only generate g directly. drawString (rand, 13 * I + 6, 16);} session. setAttribute ("rand", sRand); g. dispose (); ServletOutputStream responseOutputStream = response. getOutputStream (); ImageIO. write (image, "JPEG", responseOutputStream); responseOutputStream. flush (); responseOutputStream. close ();}/*** The doPost method of the servlet. <br> ** This method is called when a form has its tag value method equals to post. ** @ param request the request send by the client to the server * @ param response the response send by the server to the client * @ throws ServletException if an error occurred * @ throws IOException if an error occurred */public void doPost (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {doGet (request, response);}/*** Initialization of the servlet. <br> ** @ throws ServletException if an error occurs */public void init () throws ServletException {// Put your code here} www.2cto.com Color getRandColor (int fc, int bc) {// obtain the Random color random 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: