请输入验证码优化版

先写一个类继承HttpServlet

package com.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ValCode")
public class ValCode extends HttpServlet {
    
    private static final long serialVersionUID = 1L;
    
    private static String ranCode1;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //response.setContentType("text/html; charset=utf-8");
        //响应类型为图片类型的加上这一句
        response.setContentType("image/jpeg");
        //设置响应头控制浏览器不缓存图片数据
        //response.setDateHeader("expries", -1);
        //response.setHeader("Cache-Control", "no-cache");
        //response.setHeader("Pragma", "no-cache");
        /**
         * 验证码的要求:
         *     1, 验证码随机取
         * 2, 背景颜色随机变化
         * 3, 字体倾斜
         * 4, 多条干扰线
         * 5, 有边框
         */
        int width=120, height=30; 
        BufferedImage bufImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //也可以使用bufImg.getGraphics(), get是返回一个Graphics对象, create是返回一个Graphics2D对象
        Graphics2D g = bufImg.createGraphics();
        //画出图片验证码的背景(200到250是一个浅色调的背景)
        g.setColor(this.getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        //画出图片验证码的干扰线, 给出两个点的坐标, 两点确定一条直线
        for (int i = 0; i < 10; i++) {
            g.setColor(this.getRandColor(160, 200));
            int x1 = new Random().nextInt(width/2);
            int y1 = new Random().nextInt(height);
            int x2 = new Random().nextInt(width);
            int y2 = new Random().nextInt(height);
            g.drawLine(x1, y1, x2, y2);
        }
        //画一个边框
        g.setColor(Color.RED);
        g.drawRect(1, 1, width-2, height-2);
        //画出图片验证码的内容
        String data = this.getCodeContent();
        int _x = 5;
        for (int i = 0; i < data.length(); i++) {
            int deg = new Random().nextInt() % 30;
            g.setColor(this.getRandColor(20, 130));// 20到130之间是深色调
            g.rotate(deg*Math.PI/180, _x, 20);
            g.setFont(new Font("宋体", Font.BOLD, 20));
            g.drawString(data.charAt(i)+"", _x, 20);
            //这里必须设置一个恢复旋转角度的代码, 否则后面的内容会显示到边框的外面
            g.rotate(-deg*Math.PI/180, _x, 20);
            _x+=width/data.length();
        }
        ImageIO.write(bufImg, "jpg", response.getOutputStream());
    }
    
    /**
     * 获取一个随机的颜色
     */
    public Color getRandColor(int from, int end) {
        Random random = new Random();
        if(from > 255) {
            from = 255;
        }
        if(end > 255) {
            end = 255;
        }
        int red, green, blue;
        //定义随机数的范围取色
        red = from + random.nextInt(end - from);
        green = from + random.nextInt(end - from);
        blue = from + random.nextInt(end - from);
        
        return new Color(red, green, blue);
    }
    
    /**
     * 获取随机的验证码内容(字母加数字)
     */
    private String getCodeContent() {
        String str111 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        String code = "";
        for(int x = 1; x <= 6; x++) {
            Random a = new Random();
            int m = a.nextInt(62);
            code+=str111.charAt(m);
        }
        this.setRanCode(code);
        return code;
    }
    /**
     * 获取随机的验证码内容(汉字)
     */
    private String getCodeContent1() {
        Random ran = new Random();
        final int delta = 0x9fa5 - 0x4e00 + 1;
        String code = "";
        for(int i = 0; i < 4; i++) {
            code+=(char)(0x4e00 + ran.nextInt(delta)); 
        }
        this.setRanCode(code);
        return code;
    }

    public static String getRanCode() {
        return ranCode1;
    }

    public void setRanCode(String ranCode) {
        ranCode1 = ranCode;
    }
}

这个servlet是可以直接在jsp页面上显示的

<body>
<form action="ReCode" method="post">
请输入验证码: <input name="ranCode" type="text" value="" /><input type="submit" value="提交" /><br /><br />
<img id="img" src="ValCode" alt="图形验证码" width=242 height=62 onclick="return changeCode();" />
</form>
<script type="text/javascript">
    function changeCode() {
        document.getElementById("img").src="ValCode?"+Math.random();
    }
</script>
</body>

接收验证码的servlet

package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ReCode
 */
@WebServlet("/ReCode")
public class ReCode extends HttpServlet {
    
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReCode() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        
        String ranCode = request.getParameter("ranCode");
        
        if(ValCode.getRanCode().toLowerCase().equals(ranCode.toLowerCase())) {
            response.getWriter().append("验证码通过 ! ");
        } else {
            response.getWriter().append("验证码输入错误 ! ");
        }
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

支持点击图片刷新验证码

原文地址:https://www.cnblogs.com/wgbs25673578/p/4873521.html