Response验证码案例 January 27,2020

Posted yyanghang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Response验证码案例 January 27,2020相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>验证码点击切换</title>
    <script>
        /*
        分析:
            点击超链接或者图片,需要换一张
            1.给超链接和图片绑定单击事件
            2.重新设置图片的src属性值
     */
        window.onload=function () {//加载完毕后
            var img = document.getElementById("checkCode");
            img.onclick = function () {//绑定单击事件
                //时间戳 欺骗服务器
                var date = new Date().getTime();
                img.src="/order7_Request_Response_war_exploded/checkCodeServlet?"+date;
            }

            var a = document.getElementById("change");
            a.onclick = function () {
                //加时间戳 欺骗服务器
                var date = new Date().getTime();
                img.src="/order7_Request_Response_war_exploded/checkCodeServlet?"+date;
            }
        }

    </script>
</head>
<body>

    <img id="checkCode" src="/order7_Request_Response_war_exploded/checkCodeServlet">
    <a id="change" href="">看不清?换一张</a>
</body>
</html>

 

package web.response;

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( "/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.创建一个对象,在内存中 存放图片
        int width = 100;
        int heigth = 50;
        BufferedImage image = new BufferedImage(width,heigth,BufferedImage.TYPE_INT_RGB);
        //2.美化图片
            //2.1填充背景色
        Graphics graphics = image.getGraphics();//画笔对象
        graphics.setColor(Color.PINK);//设置画笔颜色
        graphics.fillRect(0,0,width,heigth);//填充矩形
            //2.2画边框
        graphics.setColor(Color.BLUE);
        graphics.drawRect(0,0,width-1,heigth-1);//因为画笔有1px  所以要减一
            //2.3填写验证码
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        for (int i = 1; i <=4 ; i++) {
            int index = random.nextInt(str.length());
            graphics.drawString(str.charAt(index)+"",width/5*i,heigth/2);
        }
            //2.4画干扰线
        graphics.setColor(Color.green);
        for (int i = 0; i <10 ; i++) {
            int x1 = random.nextInt(width);
            int x2 = random.nextInt(width);
            int y1 = random.nextInt(heigth);
            int y2 = random.nextInt(heigth);
            graphics.drawLine(x1,y1,x2,y2);
        }
        //3.将图片输出至页面展示
        ImageIO.write(image,"jpg",response.getOutputStream());

    }

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

 

以上是关于Response验证码案例 January 27,2020的主要内容,如果未能解决你的问题,请参考以下文章

Response January 27,2020

Request January 27,2020

Java 之 Response 发送验证码案例

案例-02验证码(html,servlet,Response对象)

案例-02验证码(html,servlet,Response对象)

文件下载案例 January 27,2020