Online book Store Project study notes-006 CAPTCHA function

Source: Internet
Author: User

I. Methods of Use

In 1.jsp

1       <tr>2         <td>&nbsp;</td>3         <td>4           class = "Verifycodeimg" ></span >5         </td>6         <td><a href= "javascript: _changeverifycode ()" > Change </a></td>7       </tr>8       <tr>

Configuring Servlets in 2.web.xml

1   <!--generate verification code-2   <servlet>3       <servlet-name>verifycodeservlet</ Servlet-name>4       <servlet-class>cn.itcast.vcode.servlet.verifycodeservlet</ servlet-class>5   </servlet>6   <servlet-mapping>7        <servlet-name>VerifyCodeServlet</servlet-name>8       <url-pattern>/ Verifycodeservlet</url-pattern>9   </servlet-mapping>

Second, code codes

1.verifycodeservlet.java

1  PackageCn.itcast.vcode.servlet;2 3 ImportJava.awt.image.BufferedImage;4 Importjava.io.IOException;5 6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.http.HttpServlet;8 Importjavax.servlet.http.HttpServletRequest;9 ImportJavax.servlet.http.HttpServletResponse;Ten  One ImportCn.itcast.vcode.utils.VerifyCode; A  -@SuppressWarnings ("Serial") -  Public classVerifycodeservletextendsHttpServlet { the      Public voiddoget (httpservletrequest request, httpservletresponse response) -             throwsservletexception, IOException { -Verifycode VC =NewVerifycode (); -BufferedImage image = Vc.getimage ();//Get a one-time verification code picture +         //The method must be called after the GetImage () method. - //System.out.println (Vc.gettext ());//get text on a picture +Verifycode.output (image, Response.getoutputstream ());//write the picture into the specified stream A          at         //Save the text in session to prepare for Loginservlet validation -Request.getsession (). SetAttribute ("Vcode", Vc.gettext ()); -     } -}

2.verifycode.java

1  Packagecn.itcast.vcode.utils;2 3 ImportJava.awt.BasicStroke;4 ImportJava.awt.Color;5 ImportJava.awt.Font;6 ImportJava.awt.Graphics2D;7 ImportJava.awt.image.BufferedImage;8 Importjava.io.IOException;9 ImportJava.io.OutputStream;Ten ImportJava.util.Random; One  A ImportJavax.imageio.ImageIO; -  -  Public classVerifycode { the     Private intW = 70; -     Private intH = 35; -      PrivateRandom r =NewRandom (); -      //{"Arial", "Chinese Italic", "Black Body", "Chinese New Wei", "Chinese Script", "Microsoft Ya Hei", "italics _gb2312"} +     PrivateString[] FontNames = {"Arial", "Chinese Italic", "Black Body", "Microsoft Jas", "italics _gb2312"}; -     PrivateString codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ"; +     PrivateColor BgColor =NewColor (255, 255, 255); A     PrivateString text; at      -     PrivateColor Randomcolor () { -         intRed = R.nextint (150); -         intGreen = R.nextint (150); -         intBlue = R.nextint (150); -         return NewColor (red, green, blue); in     } -      to     PrivateFont Randomfont () { +         intindex =R.nextint (fontnames.length); -String FontName =Fontnames[index]; the         intstyle = R.nextint (4); *         intSize = R.nextint (5) + 24;  $         return NewFont (fontname, style, size);Panax Notoginseng     } -      the     Private voidDrawLine (bufferedimage image) { +         intnum = 3; Agraphics2d g2 =(graphics2d) image.getgraphics (); the          for(inti = 0; i < num; i++) { +             intX1 =R.nextint (w); -             intY1 =r.nextint (h); $             intx2 =R.nextint (w); $             inty2 =r.nextint (h); -G2.setstroke (NewBasicstroke (1.5F));  - G2.setcolor (color.blue); the g2.drawline (x1, y1, x2, y2); -         }Wuyi     } the      -     Private CharRandomchar () { Wu         intindex =R.nextint (Codes.length ()); -         returnCodes.charat (index); About     } $      -     Privatebufferedimage createimage () { -BufferedImage image =NewBufferedImage (W, H, Bufferedimage.type_int_rgb); -graphics2d g2 =(graphics2d) image.getgraphics (); AG2.setcolor ( This. BgColor); +G2.fillrect (0, 0, W, h); the          returnimage; -     } $      the      Publicbufferedimage getImage () { theBufferedImage image =createimage (); thegraphics2d g2 =(graphics2d) image.getgraphics (); theStringBuilder SB =NewStringBuilder (); -         //Draw 4 characters into a picture in          for(inti = 0; I < 4; i++)  { theString s = Randomchar () + "";  the Sb.append (s); About             floatx = i * 1.0F * W/4;  the G2.setfont (Randomfont ()); the G2.setcolor (Randomcolor ()); theG2.drawstring (S, x, h-5);  +         } -          This. Text =sb.tostring (); the drawLine (image);Bayi         returnimage;  the     } the      -      PublicString GetText () { -         returntext; the     } the      the      Public Static voidoutput (bufferedimage image, outputstream out) the                 throwsIOException { -Imageio.write (Image, "JPEG", out); the     } the}

Web Book Mall Project study notes-006 CAPTCHA function

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.