js多种方式实现验证码的切换

1.点击图片切换验证码

页面1:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>验证码切换</title>
    <script>
       //第一种:点击图片执行切换
        window.onload = function (){
            //获取图片对象
            var img = document.getElementById("checkedText");
            img.onclick = function () {
                img.src = "/responseDemo01?time="+new Date().getTime();
            }
        }
    </script>
</head>
<body>
<img src="/responseDemo01" id="checkedText"></img>
</body>
</html>

使用onload等页面加载完成再执行函数体,获取图片对象,通过绑定点击事件,当点击时触发函数体,执行图片路径的请求发送,加上时间戳,使每次产生的验证码不同.

2.点击图片旁边的超链接切换验证码

页面2:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>验证码切换</title>
    <script>

        //第二种:点击链接切换验证码
        //给图片和超链接绑定单击事件
        var img;
        window.onload = function () {
            //获取图片对象
            img = document.getElementById("checkedText");
            //绑定单击事件
            img.onclick = change();

        };
        function change(){
            //加时间戳
            var date = new Date().getTime();
            img.src = "/responseDemo01?time=" + date;
        }
    </script>
</head>
<body>

<img src="/responseDemo01" id="checkedText"></img>
<a href="javascript:void(0)" onclick="change()">看不清请换一张?</a>
</body>
</html>
  1. 验证码生成的servlet
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;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet("/responseDemo01")
public class CheckCodeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int width = 100;
        int height = 50;
        //1.创建一个对象,在内存中图片(验证码图片对象)
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        //2.美化图片
        //2.1填充背景色
        Graphics g = image.getGraphics();//画笔对象
        //设置画笔颜色
        g.setColor(Color.PINK);
        //填充图片
        g.fillRect(0, 0, width, height);

        //2,2画边框
        g.setColor(Color.BLACK);
        g.drawRect(0, 0,  width - 1, height - 1);

        //2.3 写验证码
        String string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnoqrstuvwxyz0123456789";
        //生成随机角标
        Random r = new Random();

        for (int i = 1; i <= 4; i++) {
            int index = r.nextInt(string.length());
            //获取字符
            char ch = string.charAt(index);
            //写验证码
            g.drawString(ch + "", width / 5 * i,height/2 );
        }
        //画干扰线
        g.setColor(Color.GREEN);
        //随机生成坐标点
        for (int i = 0; i < 10; i++) {
            int x1 = r.nextInt(width);
            int x2 = r.nextInt(width);
            int y1 = r.nextInt(height);
            int y2 = r.nextInt(height);
            g.drawLine(x1,y1,x2,y2);
        }

        //3. 将图片展示到页面上输出
        ImageIO.write(image, "png", response.getOutputStream());

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

Java小白修炼手册

你可能感兴趣的:(前端,javascript,html)