Verification code implementation --- (servlet verification code)

Source: Internet
Author: User

Step 1: create a dynamic project check, a check. Java (servlet class), and a login. jsp file

Step 2: Compile the check. Java file with the following content:

01.package check;  02.import javax.servlet.ServletException;  03.import javax.servlet.http.*;  04.import java.io.*;  05.import java.awt.*;  06.import java.awt.image.*;  07.import java.util.*;  08.import javax.imageio.*;   09.  10./** 11. * @author  yeeku.H.lee kongyeeku@163.com 12. * @version  1.0 13. * <br>Copyright (C), 2005-2008, yeeku.H.Lee 14. * <br>This program is protected by copyright laws. 15. * <br>Program Name: 16. * <br>Date:  17. */  18.public class check extends HttpServlet  19.{  20.    private Font mFont = new Font("Arial Black", Font.PLAIN, 16);  21.    public void init() throws ServletException  22.    {  23.        super.init();  24.    }  25.    Color getRandColor(int fc,int bc)  26.    {  27.        Random random = new Random();  28.        if(fc>255) fc=255;  29.        if(bc>255) bc=255;  30.        int r=fc+random.nextInt(bc-fc);  31.        int g=fc+random.nextInt(bc-fc);  32.        int b=fc+random.nextInt(bc-fc);  33.        return new Color(r,g,b);  34.    }  35.  36.    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  37.    {  38.        response.setHeader("Pragma","No-cache");  39.        response.setHeader("Cache-Control","no-cache");  40.        response.setDateHeader("Expires", 0);  41.        response.setContentType("image/jpeg");  42.          43.        int width=100, height=18;  44.        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  45.          46.        Graphics g = image.getGraphics();  47.        Random random = new Random();  48.        g.setColor(getRandColor(200,250));  49.        g.fillRect(1, 1, width-1, height-1);  50.        g.setColor(new Color(102,102,102));  51.        g.drawRect(0, 0, width-1, height-1);  52.        g.setFont(mFont);  53.  54.        g.setColor(getRandColor(160,200));  55.        for (int i=0;i<155;i++)  56.        {  57.            int x = random.nextInt(width - 1);  58.            int y = random.nextInt(height - 1);  59.            int xl = random.nextInt(6) + 1;  60.            int yl = random.nextInt(12) + 1;  61.            g.drawLine(x,y,x + xl,y + yl);  62.        }  63.        for (int i = 0;i < 70;i++)  64.        {  65.            int x = random.nextInt(width - 1);  66.            int y = random.nextInt(height - 1);  67.            int xl = random.nextInt(12) + 1;  68.            int yl = random.nextInt(6) + 1;  69.            g.drawLine(x,y,x - xl,y - yl);  70.        }  71.  72.        String sRand="";  73.        for (int i=0;i<6;i++)  74.        {  75.   String tmp = getRandomChar();  76.            sRand += tmp;  77.            g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));  78.         g.drawString(tmp,15*i+10,15);  79.        }  80.  81.        HttpSession session = request.getSession(true);  82.        session.setAttribute("rand",sRand);  83.        g.dispose();  84.        ImageIO.write(image, "JPEG", response.getOutputStream());  85.    }  86.    private String getRandomChar()  87.    {  88.  int rand = (int)Math.round(Math.random() * 2);  89.  long itmp = 0;  90.  char ctmp = '\u0000';  91.  switch (rand)  92.  {  93.   case 1:  94.    itmp = Math.round(Math.random() * 25 + 65);  95.    ctmp = (char)itmp;  96.    return String.valueOf(ctmp);  97.   case 2:  98.    itmp = Math.round(Math.random() * 25 + 97);  99.    ctmp = (char)itmp;  100.    return String.valueOf(ctmp);  101.   default :  102.    itmp = Math.round(Math.random() * 9);  103.    return String.valueOf(itmp);  104.  }  105.    }  106.}  

Step 3: Compile the login. jsp file with the following content:

01. <% @ page Language = "Java" Import = "Java. util. * "pageencoding =" gb18030 "%> 02. <% 03. string Path = request. getcontextpath (); 04. string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; 05. %> 06. 07. <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> 08. <HTML> 09. 

Step 4: configure the code in the web. xml file with the following content:

01.<servlet>  02.        <servlet-name>img</servlet-name>  03.        <servlet-class>check.check</servlet-class>  04.    </servlet>  05.  06.    <servlet-mapping>  07.        <servlet-name>img</servlet-name>  08.     <url-pattern>/check</url-pattern>  09.    </servlet-mapping>   

 

Step 5: Release the project check on the Tomcat server and run login. jsp. The running result is:

 

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.